zoukankan      html  css  js  c++  java
  • day16-封装(私有静态属性、私有属性、私有方法、类方法、静态方法)

    # 一:
    class P:
        __age = 30    #私有静态属性
        def __init__(self,name):
            self.__name = name  #私有属性:属性名前面加上双下划线是私有属性。
    
        def get_name(self):
            return self.__name
    
        def __func(self):  #私有方法
            print('私有方法')
    p = P('alex')
    # print(p.__name)  #报错,在类的外面,不能使用这个方法来调用私有属性。
    print(p._P__name)  #alex, 私有属性使用 对象._类名__属性名 调用才可以。
    print(p.get_name()) #alex,调用方法可以返回私有属性。
    print(p._P__age)  #30,私有静态属性也是通过 对象._类名__属性名 调用。
    print(p._P__func()) #私有方法通过 对象._类名__方法名() 调用。
    
    # 二:
    # 私有概念的作用:1.保护属性,不让它在类的外面被调用或者修改。2.不想被子类继承。
    #父类的私有静态属性、私有属性、私有方法无法被子类继承:
    class Game:
        __country = 'CHINA'          #私有静态属性
        def __init__(self,name):     #私有属性
            self.__name = name
        def __aggr(self):            #私有方法
            print('攻击力是100')
    class Son(Game):pass
    
    s = Son('alex')
    print(s._Son__country) #报错
    print(s._Son__name)#报错
    print(s._Son__aggr())#报错
    
    # 三: @property
    #超市商品:
    class Goods:
        def __init__(self,name,price,add_price,discount):#商品名字、进货价、加价、折扣
            self.name = name
            self.__price = price
            self.add_price = add_price
            self.discount = discount
        @property   #此装饰器把方法伪装成属性。
        def price(self):
            return (self.__price + self.add_price)* self.discount
    
    g = Goods('苹果',5,3,0.8)
    print(g.price) #6.4,调用方法就像调用属性一样,省略括号()。
    
    # 四、私有属性的删改查:
    # 4.1删除私有属性:对象的私有属性被删除之后,类的私有属性也被删除。
    class Goods:
        def __init__(self,name,price):
            self.name = name
            self.__price = price
        @property
        def price(self):
            return self.__price  #返回值self.__price等于price()
        @price.deleter         #上和下两个方法的方法名必须跟此处的price同名才可以删除私有属性。
        def price(self):
            del self.__price  # self.__price等于apple.price
    apple = Goods('苹果',5)
    print(apple.price)
    del apple.price  #del触发上面的deleter,接着调用price方法来删除私有属性。
    print(apple.price) #'Goods' object has no attribute '_Goods__price',说明对象的私有属性已经被删除。
    print(apple._Goods__price)#'Goods' object has no attribute '_Goods__price',类的私有属性也被删除了。
    
    # 4.2修改私有属性: 修改是共享的,对象的私有属性被修改之后,类的私有属性也会被修改。
    class Goods:
        def __init__(self,name,price):
            self.name = name
            self.__price = price
        @property
        def price(self):
            return self.__price    #返回值self.__price等于price
        @price.setter              #上和下两个方法的方法名必须跟此处的price同名才可以修改私有属性。
        def price(self,new_price):
            self.__price = new_price # self.__price等于apple.price
    
    apple = Goods('苹果',5)
    print(apple.price) #5
    apple.price = 8    #方法伪装成属性,看起来像属性修改,然后把8传给new_price。此操作是修改,而不是单独赋值。
    print(apple.price) #8,修改是共享的,所以对象的私有属性和类的私有属性都被修改为8。
    print(apple._Goods__price)#8,类的私有属性被修改为8。
    
    #4.3修改私有静态属性: 修改原理跟修改私有属性一样,但是需要依靠实例化对象。苹果的折扣修改了,导致所有商品的折扣都被修改了,这是不合理的。
    class Goods:
        __discount = 1 #私有静态属性
        @property
        def discount(self):
            return Goods.__discount  #Goods.__discount = discount
        @discount.setter
        def discount(self,new_discount):
            Goods.__discount = new_discount #Goods.__discount = apple.discount
    
    apple = Goods()
    print(apple.discount) # 1
    apple.discount = 0.8
    print(apple.discount) # 0.8
    print(apple._Goods__discount) # 0.8,苹果的折扣修改了,导致所有商品的折扣都被修改了,这是不合理的。
    
    #4.4 @classmethod类方法。商品的折扣修改了,苹果的折扣也随着被修改,这是合理的。
    class Goods:
        __discount = 0.8
        def __init__(self,name,price):
            self.name = name
            self.__price = price #price是进货价,使用私有属性保护起来,不想在类的外面被修改。
        @property
        def price(self):
            return self.__price * Goods.__discount # 售价=进货价*折扣
        @classmethod   #类方法可以修改私有静态属性,不需要依靠实例化对象apple。
        def discount(cls,new_discount): #对象是cls,所以这个方法里面不能有私有属性,因为私有属性的对象是self。
            cls.__discount = new_discount
    
    apple = Goods('apple',5)
    print(apple.price) # 4.0
    Goods.discount(0.7)
    print(apple._Goods__discount) # 0.7
    print(apple.price) # 3.5
    
    # 五、静态方法:@staticmethod跟对象和类没关系,apple()括号里面没有self。
    class Goods:
        @staticmethod #静态方法
        def apple():
            print('苹果')
    g = Goods()
    g.apple() #苹果
    Goods.apple()#苹果
  • 相关阅读:
    算法 quick sort
    非常多学ThinkPHP的新手会遇到的问题
    原创jQuery插件之图片自适应
    管理线程之向线程函数传递參数
    [LeetCode]94.Binary Tree Inorder Traversal
    数据库基本概念
    Eclipse4.4 安装java反编译插件Eclipse Class Decompiler
    广义线性模型
    代理模式
    多态之中的一个(继承和虚函数)
  • 原文地址:https://www.cnblogs.com/python-daxiong/p/11229495.html
Copyright © 2011-2022 走看看