Fields, methods, properties
1 #static fields and normal fields2 classFoo:3 #static fields4Contry ='China'5 def __init__(self,name):6 #normal field7Self.name =name8 9 #calls to normal fields require an object invocation, which requires instantiation of the objectTenHN = Foo ('Henan') One Print(Hn.name) A>>>Henan -HB = Foo ('Hebei') ->>>Hebei the #static fields are called by the class without instantiating the object.Static Field objects and classes in Python can access static fields in the class, and generally do not apply to static fields called by objects
- Print >>> China
#补充静态字段, the code is loaded when it is created, and the normal field is invoked when the object is instantiated
#construction methods, static methods, common methods, class methodsclassFoo:#Construction Method def __init__(self,name): Self.name=name#common method, called by the object, belongs to the class defShow (self):Print(Self.name)#static methods, which belong to a class, cannot be called by an object, are called with a class #static methods use static methods when methods in a class do not call an object, as with functions@staticmethoddefF1 (args):Print(args) @classmethoddefF2 (CLS): Ret= CLS ('Alex') ret.show ()#Common methods use object utilityobj = Foo ('Liguangxu') obj.show ()>>>Liguangxu#static methods use class invocationFOO.F1 ('ABC')>>>ABC#Class methods use the class invocation, which by default passes the class name as a parameter, (must have) to initialize the object in the class. foo.f2 ()>>>alex
day8-python-Object-Oriented 2