day16- package (private static property, private property, private methods, class methods, static methods)

# A: 
class P:
     __age = 30     # private static properties 
    DEF  the __init__ (Self, name): 
        . Self __name__ = name   # private attributes: Attribute name with front double underlined private property. 

    DEF get_name (Self):
         return Self. __name__ 

    DEF  __func (Self):   # Private Method 
        Print ( ' Private method ' ) 
P = P ( ' Alex ' )
 # Print (P .__ name) # error, outside the class, not use this method to call the private property. 
Print (p._P__name)   #alex, private property using the object class name ._ __ attribute name can only be invoked. 
Print (p.get_name ()) # alex, call the method can return private property. 
Print (p._P__age)   # 30, a private static property is also subject to call by name __ ._ class property name. 
Print (p._P__func ()) # private method () call through the object class name __ ._ method name. 

# Two: 
# the concept of private role: 1 protection attribute, do not let it out of the class is called or modified. 2. do not want inherited by subclasses. 
# Private static properties of the parent class, private property, private methods can not be inherited by subclasses: 
class Game:
     __country = ' CHINA '           # private static properties 
    DEF  __init__ (Self, name):      # private property 
        . Self __name__ = name
     DEF __aggr (Self):             # Private Method 
        Print ( ' attack is 100 ' )
 class Son (Game): Pass 

S = Son ( ' Alex ' )
 Print (s._Son__country) # error 
Print (s._Son__name) # error 
Print ( s._Son__aggr ()) # error 

# three: the @Property 
# supermarket goods: 
class goods:
     DEF  __init__ (Self, name,. price, add_price, the discount): # commodity name, purchase price, fare, discount 
        self.name = name
        Self. __price = . price 
        self.add_price = add_price 
        self.discount = Discount 
    @Property    # this method the decorator disguised properties. 
    DEF . price (Self):
         return (. Self __price + self.add_price) * self.discount 

G = Goods ( ' apple ' , 5,3,0.8 )
 Print (g.price) # 6.4, calling the same method as calling attribute , omit the parentheses (). 

# Fourth, the private property of excision investigation: 
# 4.1 Delete Private property: private property after the object is deleted, the class of private property is also deleted. 
class Goods:
     DEF  __init__(Self, name,. price): 
        the self.name = name 
        Self. __price = . price 
    @Property 
    DEF . price (Self):
         return . Self __price   # return value equals self .__ price. price () 
    @ price.deleter          # upper and lower two method name method must be before they can delete private property with the price of the same name here. 
    DEF . price (Self):
         del . Self __price   # Self .__. price equal apple.price 
Apple Goods = ( ' apple ' ,. 5 )
 Print (apple.price)
 del apple.price   #del triggered above deleter, then call price method to remove private property. 
Print (apple.price) # 'Goods' Object attribute has NO '_Goods__price', explained the private property of the object has been deleted. 
Print (apple._Goods__price) # 'Goods' Object attribute has NO '_Goods__price', like private property has been deleted. 

# 4.2 modify private property: Change is shared, then the private property of the object is modified, the class of private property will be modified. 
class Goods:
     DEF  the __init__ (Self, name, price): 
        the self.name = name 
        Self. __price = price 
    @Property 
    DEF price (Self):
         return . Self __price     # return self .__ price equal to the price value
    price.setter @               # the method name and under the two methods must be modified only with the price of private property the same name here. 
    DEF . price (Self, new_price): 
        . Self __price = new_price # Self .__. price equal apple.price 

Apple = Goods ( ' apple ' ,. 5 )
 Print (apple.price) # . 5 
apple.price. 8 =     # Method disguised properties, It looks like a property modification, and then pass the 8 new_price. This operation is to modify, rather than assigned individually. 
Print (apple.price) # 8, modifications are shared, private object class attributes and private attributes are modified to 8. 
Print (apple._Goods__price) # 8, class 8 is modified to a private property. 

#4.3 modify private static properties: Modify the principle of private property with the same modifications, but need to rely on instantiate an object. Apple discounts modified, resulting in discounts of all commodities have been modified, it is unreasonable. 
class Goods:
     __discount = 1 # private static properties of 
    the @Property
     DEF the Discount (Self):
         return Goods. __discount   # Goods .__ the Discount the Discount = 
    @ discount.setter
     DEF the Discount (Self, NEW_DISCOUNT): 
        Goods. __discount = NEW_DISCOUNT # Goods .__ the Discount = apple.discount 

Apple = Goods ()
 Print (apple.discount) # . 1 
apple.discount = 0.8 Print (apple.discount)
# 0.8 
Print (apple._Goods__discount) # 0.8, Apple changed the discount, resulting discount on all items have been modified, it is unreasonable. 

# 4.4 @classmethod class method. Discount merchandise changed, along with Apple's discount is modified, it is reasonable. 
class Goods:
     __discount = 0.8
     DEF  the __init__ (Self, name,. price): 
        the self.name = name 
        Self. __price =. price # . price is the purchase price, the use of private property protected, not want to be modified outside the class. 
    the @Property
     DEF . price (Self):
         return . Self __price * Goods. __discount  # price = purchase price discount * 
    @classmethod    #Private static class method may modify the properties, without relying object instance apple. 
    DEF the Discount (cls, NEW_DISCOUNT): # objects are cls, so this method can not have inside a private property, private property because the object is 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 of 

# five static methods : @staticmethod with objects and classes does not matter, apple () brackets no self. 
class Goods: 
    @staticmethod # static method 
    defApple ():
         Print ( ' apple ' ) 
G = Goods () 
g.apple () # Apple 
Goods.apple () # Apple

 

Guess you like

Origin www.cnblogs.com/python-daxiong/p/11229495.html