# Object = attribute + method
>>> # Class name conventions in Python start with uppercase letters
>>> # TT = Turtle () This is the way to create class instances, other languages with new, which is not required
>>>
>>> # The self in Python is equivalent to this in Java
>>> # Self , usually put in the first parameter of the method this is the default requirement
classBall :defSetName (self,name): Self.name=namedefKick (self):Print("My name is%s, who kicked me and found death ..."%self.name) A= Ball () # No need to use the New keyword, notice the difference with other languages A.setname ("Huang") b=Ball () b.setname ("Huanghongyi") A.kick () My name is Huang, who kicks me and dies ... b.kick () My name is Huanghongyi, who kicks me, find death ...
>>>
>>> # Python's Magic method, which is automatically called by Python at special Times
>>> # One feature of these magic methods is that they are surrounded by double underscores
>>> # such as __init__ (self)
>>> # __ init__ (Self,param1,param2,...), is a construction method that is automatically called when an object is instantiated
>>>
class Ball : def __init__ (self,name): = name def Kick (self): print(" My name is%s, who kicks me and dies ...) " %self.name) = Ball (' potatoes ') B.kick () My name is potatoes, Who kicked me, looking for death ...
>>>
>>># To define a private variable or function in Python, just add two underscore "__" before the name
>>>classPerson :__name='Pig'>>> p =Person ()>>> p.__nameTraceback (most recent): File"<pyshell#45>", Line 1,inch<module>p.__nameAttributeerror:' Person'object has no attribute'__name'
>>>
>>># in fact,Python is the name of a private property and method that starts with __ class name __ Variable Name
>>> # From this point, it can be seen thatthe privatization of Python is a pseudo-private
class Person : __name ' Pig ' def GetName (self): return self. __name >>> p = person ()>>> p.getname ()' pig '> >> >>> p._person__name' pig '
Python Learning Notes 008_ Class _ Object