python_ Object Oriented - Encapsulated

1. Private property

class the Person (Object):
     DEF  the __init__ (Self, name, Age): 
        the self.name = name 
        self.age = Age   # instance attribute 
        self.attack_val 30 = 
        Self. __life_val = 100    # preceded by two private variables is underlined, Private attribute 


A = the Person ( ' WDC ' , 22 is )
 Print (a.age)     # instance attribute may be modified and used directly out of 
Print (A. __life_val ) # outside a private property may not be used and modified

 

 2. The use of private property

class the Person (Object):
     DEF  the __init__ (Self, name, Age): 
        the self.name = name 
        self.age = Age   # instance attribute 
        self.attack_val 30 = 
        Self. __life_val = 100    # preceded by two private variables is underlined, Private properties 
    DEF get_life_val (Self):
         Print (. Self __life_val )   # can be called within the method class 
        return Self. __life_val   # may be returned within the method of the class attribute value 


a = the Person ( ' WDC ' , 22 is ) 
BA.get_life_val = ()     # call the private property method, and receives a return value 
Print (B)

 

3.

 

Guess you like

Origin www.cnblogs.com/wangdianchao/p/11895539.html