zoukankan      html  css  js  c++  java
  • Day 26封装

    一、封装

    广义上的封装: 属于一个类的静态和动态属性,总是出现在一个类中. 

           使用的永远用类名或者对象名调用.

    狭义上的封装:就是把变量和方法私有化,在类的外部以及子类中不能直接使用了 。

    class A:
        STATIC ='aaa'#静态变量
        __S ='bbbb'#私有的静态变量
        def wahaha(self):
            print(A.__S)#_A__S 系统自动加了一个_A
    print(A.STATIC)  #打印静态变量
    print(A.__dict__) #以字典的形式打印出来
    print(A._A__S)# 在类的外面调用是有的变量.
    a= A()
    a.wahaha()
    A.__B=("ccc") #在类的外部添加一个静态变量.(此变量不是私有变量)
    print(A.__dict__)
    # 打印结果
    # {'__module__': '__main__', 'STATIC': 'aaa', '_A__S': 'bbbb', 'wahaha': <function A.wahaha at 0x0058D6A8>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>,
    '__doc__': None, '__B': 'ccc'}
     

    私有类能够在内部随意调用,但不建议在外部调用

    class B:
        def __init__(self,name,pwd):
            self.name =name
            self.__pwd =pwd #也可以创建对象的私有属性.
        def qqxing(self):
            self._a ="A" #_B__a
        def get_pwd(self):
             print(self.__pwd) # B._B__pwd
    b= B('alex','alex1234')
    b.qqxing()
    print(b.name) #对象调用属性.
    # print(b._B__pwd) #当在类外部的时候,外面不建议直接使用对象的私有属性
    # b.get_pwd()  
    class C:
        def __ppt(self): #私有的方法
            print("ppt")
            self.age =83
        def open(self): #内部调用私有方法.
            self.__ppt()
            print("打开文件")
    
    c=C()
    c._C__ppt() #也可以调用. 不建议在外面调用,要在内部调用.
    c.open()# 通过open方法去调用内部私有方法.

    私有的 静态变量  对象属性 动态方法

    # 私有的变量  只能在类的内部定义和使用

    #__变量 

    #在类的外部使用私有静态变量_类名__私有变量名.

    class D:
        __DDD ='ddd'  #_D__DD
        AAA='aaa'
    class E(D):
        def qqxing(self):
            print(E.AAA)# 可以调用
            print(E.__DDD) #不可以调用
    
    e =E()
    e.qqxing()

    #私有的名字 不能被子类继承.

    java 

    # private 私有的  -- __变量名 

    #protect 保护的 --__N/A

    #public 公共的 --正常的变量

    # 房间类:所有人 价格 面积
    class  Room :
        def __init__(self,owner,price,length,width,height):
            self.owner =owner
            self.__price_single =price #单价 ,私有对象属性
            self.__length  =length #私有对象属性
            self.__width =width #私有对象属性.
            self.height =height
    
        def get_area(self):
            return self.__length * self.__width
        def get_price(self):
            return self.__price_single * self.get_area()
    alex =Room ('alex ',1000,2,1,0.3)
    print(alex.get_area())
    print(alex.get_price())
    答案:
    2
    2000

     Property

    #属性
    property   #重要程度五颗星
    classmethod #重要程度三颗星
    staticmethod #重要程度一颗星
    class Person:
        def __init__(self,name,height,weight):
            self.name =name
            self.__height =height
            self.__weight =weight
        @property #将一根方法伪装成属性
        def bmi(self):
            return self.__weight / (self.__height**2)
    wang =Person('王子',1.77,69)
    print(wang.bmi)

    classmethod

    class Goods:
        __discount = 0.8
        def  __init__(self,name,price):
            self.name = name
            self.__price = price
    
        @property
        def price(self):
            return self.__price*Goods.__discount
    
        @classmethod  #讲一个普通方法装饰为一个类方法
        def change_discount(cls,new_dis):  # 类方法
            cls.__discount = new_dis
    Goods.change_discount(1) #类名调用 类方法 ①
    cig = Goods('cigrette',20)
    print(cig.price)
    cig.change_discount(0.2)
    print(cig.price)
    # cig.change_discount(1)  #对象名调用方法 ②
    # print(cig.price)

    #类方法是被@classmethod装饰的特殊方法
    #被装饰之后,方法默认接收一个 类 作为参数
    # 之后所有的操作都只能和 类中的静态变量相关 而不应该和对象相关
    # 类名 和 对象名 都可以直接调用类方法

    staticmethod

    class Student:
        def __init__(self,name):
            self.name = name
    
        @staticmethod    #装饰一个不需要self参数 也不需要cls参数的函数
        def login(a,b,c):    # 普通的函数
            usr = input('username>>>')
            pwd = input('password>>>')
            if usr == 'alex' and pwd == '123':
                obj = Student(usr)
                return obj
    # 学生 login
    # 用户名  密码  属性
    ret = Student.login(1,2,3)
    print(ret)
  • 相关阅读:
    Android安全机制
    service不死之身
    图片加载机制比较
    handler机制面试
    SharedPreferences封装类
    文字太长自动缩小
    ANR
    onCreate源码分析
    线程池的启动策略
    Oauth认证协议
  • 原文地址:https://www.cnblogs.com/mengbin0546/p/8534062.html
Copyright © 2011-2022 走看看