Members of the Class 1. Fields
Static fields and Normal fields
# class foo:# country = ' China ' # static field, belongs to class, code is loaded when you have created # def __init__ (self,name): # Self.name = name # Normal field, belonging to the # Ordinary Field object can only be accessed with the object # static field (try to avoid using objects) HN = foo (' henan ') HB = foo (' Hebei ') print (foo.country) In-Memory storage: Static fields save only one copy of the normal field in memory a static field scenario is required for each object: When you create an object from a class, you use a static field if each object has the same field
2. Methods
1. Common method: At least one self, the object invokes execution
2. Static method: Arbitrary parameter, class call execution
3. Class method : At least one CLS, class call execution
For all methods, all belong to the class, so only one copy is stored in memory
Class Foo: def __init__ (self,name): self.name = name# Normal method, object invocation execution, belongs to class def num (self): print ( Self.name) #把不需要通过对象调用执行的普通方法做一些改动, becomes a static method (the normal function in Python) and executes @staticmethod def Show (ARG) by invoking the class directly: Print (arg, ' static method ') #类方法, no arguments required, @classmethod def clas (CLS): a = CLS (' a ') a.num () print (CLS) Foo.show (' This is ') obj = Foo (' This is the normal method ') Obj.num () Foo.clas ()
3. Properties
Object Oriented (ii)