Features
Class
Object
Encapsulation
Inheritance
Polymorphism
Syntax
Call a function -- execute -- return results
R1 = role. _ init _ () return x342423
R1 = role (R1, "A", "police", "15000 ")
R1.name = ""
R1.role = "poice"
R1.monetary = 15000
R1.buy _ gun () role. buy_gun (R1)
Attribute
Method
What is the usage of class variables? Attribute shared by everyone to save costs
Class person:
CN = "China"
Def _ init _ (self, name, age, ADDR, Cn = "China ")
Self. Name = Name
Self.cn = Cn
P1 = person (name, age, ADDR)
Constructor
Destructor: it is automatically executed when an instance is released or destroyed. It is usually used for some final work, such as closing some database connections and closing open temporary files.
Private method, private property
Class variable
Instance variables
Encapsulation
Inheritance
Py2 classic classes are inherited by depth first, while new classes are inherited by breadth first.
Py3 classic classes and new classes are inherited by breadth first.
Polymorphism
One interface and multiple implementations
Class
Attribute
Instance variables
Class variable
Private Attribute _ VaR
Method
Constructor,
Destructor
Private Method
Object: the object obtained after instantiating a class
Encapsulation
Expose the implementation details of some functions
Inheritance
Inheritance, combination
Code reuse
Single inheritance
Multi-inheritance,
2.7 classic category, depth first, new category, breadth first
3. X is breadth-first
Class Foo (object ):
Def _ init _ (self, name, age, sex, salary, course ):
Self. Salary = salary
Self. Course = Course
Polymorphism
Interface reuse, one interface, multiple implementations
Static Method
Only nominal Classification Management. In fact, the static method cannot access any attributes of the class or instance.
Class Method
Only class variables are allowed. instance variables cannot be accessed.
Attribute Method
Convert a method into a static property
Reflection
Hasattr (OBJ, name_str) is used to determine whether the corresponding name_str string exists in the object obj.
Getattr (OBJ, name_str) obtains the memory address of the corresponding method in the OBJ object based on the string.
Setattr (OBJ, 'y', Z), is equivalent to ''x. Y = v''
Delattr
Exception
Try:
Code
Except T (error1, erro2) as E:
Print E
Failed t exception: Catch all errors, not recommended
Object-Oriented programming knowledge point