__init__ constructor function
Self.name = Name # property, instance variable, member variable, field
def sayhi () # method, dynamic property
Private properties do not see the front plus __
classRole ():#Pass Parameters def __init__(self,name,role,weapon,life_value=100,moneny=15000): #constructor Function #initialization of some classes during instantiationSelf.name =name Self.role=role Self.weapon=Weapon Self.life_value=Life_value Self.moneny=Moneny self.__heart='Normal' #Private Properties defShot (self):#method of the class, function (dynamic property) Print("shotting") defGot_shot (self):Print("on,%s got shoot ..."%self.name)defBuy_gun (self,gun_name):Print("%s just bought%s"%(self.name,gun_name)) Self.weapon=Gun_nameb1= Role ('Mike','Police','AK47')Print(B1.name)Print(B1.__heart)#private properties are not public" "Miketraceback (most recent): File "c:/users/administrator/pycharmprojects/ss/sss/day6/private attribute 1.py", line + <module> Print (b1.__heart) Attributeerror: ' Role ' object has no attribute ' __heart '" "
Used in internal methods
classRole ():#Pass Parameters def __init__(self,name,role,weapon,life_value=100,moneny=15000): #constructor Function #initialization of some classes during instantiationSelf.name =name Self.role=role Self.weapon=Weapon Self.life_value=Life_value Self.moneny=Moneny self.__heart='Normal' #Private Properties defShot (self):#method of the class, function (dynamic property) Print("shotting") Print("Private Properties", self.__heart) self.__heart='dddd' Print("Private Properties", self.__heart) defGot_shot (self):Print("on,%s got shoot ..."%self.name)defBuy_gun (self,gun_name):Print("%s just bought%s"%(self.name,gun_name)) Self.weapon=Gun_nameb1= Role ('Mike','Police','AK47')Print(B1.shot ())" "shotting Private Property normal private property Ddddnone" "
If you want to force external access
classRole ():#Pass Parameters def __init__(self,name,role,weapon,life_value=100,moneny=15000): #constructor Function #initialization of some classes during instantiationSelf.name =name Self.role=role Self.weapon=Weapon Self.life_value=Life_value Self.moneny=Moneny self.__heart='Normal' #Private Properties defShot (self):#method of the class, function (dynamic property) Print("shotting") Print("Private Properties", self.__heart) self.__heart='dddd' Print("Private Properties", self.__heart) #define a method for external access defShow_heart (self):returnSelf.__heart defGot_shot (self):Print("on,%s got shoot ..."%self.name)defBuy_gun (self,gun_name):Print("%s just bought%s"%(self.name,gun_name)) Self.weapon=Gun_nameb1= Role ('Mike','Police','AK47')Print(B1.show_heart ())#Normal
Force access to private properties
classRole ():#Pass Parameters def __init__(self,name,role,weapon,life_value=100,moneny=15000): #constructor Function #initialization of some classes during instantiationSelf.name =name Self.role=role Self.weapon=Weapon Self.life_value=Life_value Self.moneny=Moneny self.__heart='Normal' #Private Properties defShot (self):#method of the class, function (dynamic property) Print("shotting") defGot_shot (self):Print("on,%s got shoot ..."%self.name)defBuy_gun (self,gun_name):Print("%s just bought%s"%(self.name,gun_name)) Self.weapon=Gun_nameb1= Role ('Mike','Police','AK47')#force access to private propertiesPrint(B1._role__heart) B1._role__heart#Normal
Private property Inheritance refactoring method cannot access private properties of parent class
classFoo (object):def __init__(self, name): Self.__name=namedefF1 (self):Print(self.)__name)classBar (Foo):defF2 (self):Print(self.)__name) obj= Bar ("Mike") obj.f2 ()" "Traceback (most recent): File "c:/users/administrator.qh-20170325tnqr/pycharmprojects/sssss/ss/day7/ S13 Object-oriented/private property inheritance 1.py ", line, in <module> obj.f2 () File" C:/users/administrator.qh-20170325tnqr/pycharmproject S/sssss/ss/day7/s13 Object-oriented/private property inheritance 1.py ", line F2 print (self.__name) Attributeerr
Only the parent class can access it
classFoo (object):def __init__(self, name): Self.__name=namedefF1 (self):Print(self.)__name)classBar (Foo):defF2 (self):Print(self.)__name) obj= Bar ("Mike") obj.f1 ()#Mike
Python object-oriented private properties