Object-oriented Advanced: class members, modifiers for class members, special members of classesclass member
Class members fall into three main categories: Fields, Methods, properties
First, the field
- static field (belongs to Class)
- Normal field (belongs to object)
1 classCity :2 #static fields3Country ="China"4 5 def __init__(self,city_name):6 #normal field7Self.city_name =City_name8 9 defShow (self):Ten Print(Self.city_name) One A -Obj1 = City ("Harbin") -Obj2 = City ("Shanghai") the #common Field object to access - Print(Obj1.city_name,obj2.city_name) - #static fields are accessed through the class name - Print(City.country)Field
static field definition and application: As the program executes, it disappears as the program ends, so we call it a static field when the program ' survives ' the field. It is like a global variable that does not belong to any object, we can call it directly using the class, or we can use it when the object uses the method. It is an object-shared variable that exists in the memory of the class.
Special: objects can also access static fields.
Follow the rules: Normal fields can only be accessed by objects, and static fields are accessed with classes. (with the last resort to access the object)
Second, the method
- Static methods
- Class method
- Common methods
The path to Python-Object oriented (Advanced article)