Python's face object, with 3 Members, fields, methods, properties
1 classFoo:2 def __init__(self,name):3 #The public field name can be called in the class and outside of the class4Self.name =name5 6 defF1 (self):7 Print(Self.name)8 9obj = Foo ('Alex')Ten #Internal Call One Print(Obj.name) A #>>>alex - #External calls - obj.f1 () the #>>>alex
1 classFoo:2 def __init__(self,name):3 #private field name cannot be called except in class calling class4Self.__name=name5 6 defF1 (self):7 Print(self.)__name)8 9 Tenobj = Foo ('Alex') One #Internal Call A Print(Obj.name) - #>>>alex - #External calls the obj.f1 () - #>>> Error
1 #Special Members2 classFoo:3 #Construction Method4 def __init__(self,name,age):5Self.name =name6Self.age = Age7 Print('executing when instantiating an object')8 9 #destructor MethodTen def __def__(self): One Print('automatically triggers execution when memory is released') A def __call__(self): - Print('Pager') - the def __str__(self): - return '%s-%d'%(self.name,self.age) - return 'Show me what you return' -p=Foo () +>>>executing when instantiating an object - #>>> Show which class the object belongs to + Print(p.__class__) A>>><class '__main__. Foo'> at #Execute The call method - p () ->>>Pager - #The constructor method is executed before the call method is executed - Foo () () ->>>executing when instantiating an object in>>>Pager - toObj1 = Foo ('Alex', 71) +Obj2 = Foo ('Eric', 72) - #The print object automatically calls the __str__ method the Print(OBJ1) *>>>alex-71 $ Print(OBJ2)Panax Notoginseng>>>eric-72 - #automatically invoke the __str__ method when executing the str () method theRET =Str (OBJ1) + Print(ret) A>>>alex-71
day8-python-member modifier