I. Members of the class
The members of a class can be divided into three main categories: Fields, Methods, properties
Note: In all members, only the contents of the normal field are saved in the object, that is: how many objects are created based on this class, and how many normal fields are in memory. Other members are saved in the class, that is, only one copy is created in memory, regardless of the number of objects.
First, the field
Fields include: normal fields and static fields, they differ in definition and use, and the most essential difference is where the memory is saved in different places.
Normal fields belong to objects, static fields belong to class
classProvince:#static fieldsCountry ='China' def __init__(self, name):#normal fieldSelf.name =name#direct access to normal fieldsobj = Province ('Hebei province')PrintObj.name#direct access to static fieldsProvince.country
As can be seen from the above code, ordinary fields need to be accessed through the object, static fields through the class access, in use can be seen in the normal field and static field attribution is different. Its content is stored in a similar way as:
By:
- A static field holds only one copy in memory, and in the object, there is a class object pointer to a static field in the class
- Normal fields save one copy of each object
Scenario: When you create an object from a class, a static field is used if each object has the same field.
Second, the method
Methods include: Common methods, static methods, and class methods, three methods in memory belong to the class, the difference is that the calling method is different.
- Normal method: Called by the object; at least one self parameter; When the normal method is executed, the object that invokes the method is automatically assigned the value to auto;
- Class method: Called by the class; at least one CLS parameter; When the class method is executed, the class that invokes the method is automatically copied to the CLS;
- Static methods: Called by the class, no default parameters;
classFoo:def __init__(self, name): Self.name=namedefOrd_func (self):"""define a common method, with at least one self parameter""" #Print Self.name Print 'Common Methods'@classmethoddefClass_func (CLS):"""define a class method with at least one CLS parameter""" Print 'class Method'@staticmethoddefStatic_func ():"""define static methods with no default parameters""" Print 'Static Methods'#calling the normal methodf =Foo () f.ord_func ()#Calling class methodsFoo.class_func ()#calling a static methodFoo.static_func ()
The same point: for all methods, it belongs to the class (non-object), so only one copy is saved in memory.
different points: The method callers are different, and the parameters that are passed in automatically when the method is called are different.
Third, attribute
If you already know the methods in the Python class, the properties are very simple, because the properties in Python are actually variants of the common method .
For attributes, the following three points of knowledge are available:
- Basic use of attributes
- Two ways to define a property
1) Basic use of attributes
# ############### definition ############### class Foo: def func (self): Pass # Defining Properties @property def prop (self): pass# # # # # # # # # ########### call ###############foo_obj = foo () Foo_obj.func () Foo_obj.prop # Call Properties
The definition and invocation of a property is a few points to note:
- When defining, add @property adorners on the basis of common methods;
- When defined, the property has only one self parameter
- When called, no parentheses are required
Method: Foo_obj.func ()
Property: Foo_obj.prop
Note: The attribute has the meaning that it can be used to create and access the exact same illusion of the field when accessing the property.
Property is a variant of the method, and if there are no attributes in Python, the method can completely replace its functionality.
Python knowledge review-object oriented