Python object-oriented private properties

Source: Internet
Author: User

__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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.