Class Members:
#字段
Normal fields belong to object execution only through object access
A static field belongs to class execution that can be accessed either through an object or through a class
classFoo:def __init__(self,name):#FieldSelf.name =name#Method defShow (self):Print(self.name) obj= Foo ('Alex')Print(Obj.name)#call field, no parentheses are needed behindObj.show ()#call method, parentheses are required behind
Field instance:
classProvince:#static fields (analogy global variables)Country ='China' def __init__(self,name):#normal fieldSelf.name =Namehenan= Province ('Henan') Hebei= Province ('Hebei')Print(Henan.name)#output Result: Henan #通过对象访问Print(Province.country)#output: China #通过类访问Print(Henan.country)#output: China #通过对象访问
#方法
The normal method is saved in the class, and the object calls the argument self, which refers to the =>> call object
Static methods are saved in the class, called directly by the class, and do not require the self parameter
The class method is saved in the class, called directly by the class, and the parameter CLS refers to the =>> current class
Static MethodsclassFoo:defBar (self):Print('Bar') @staticmethoddefSTA ():Print('123') @staticmethoddefstat (A1,A2):Print(A1,A2) Foo.sta () Foo.stat (The)#Output Result:#123#1 2
# class method
class Foo: def Bar (self): Print ('bar') @classmethod def Class_f (CLS): Print(' my own class name ') foo.class_f () # access by direct invocation of class # output Result: My own class name
# The definition format of the property method, the way the field is referenced
class foo: @property def Pro (self): return 1= Foo () Print (Obj.pro) # output results: 1 The Sett property implements various functions through the adorner . del
# example: Enter page numbers to display paging practice purpose: By using attributes to reduce the last call of code, parentheses.
# The second way to express a property
Python Object-oriented advanced