6th Chapter Custom Function Unit (ii)
Defining classes
Form:
Class name (parent class name):
Pass
The parent class name is optional, does not inherit other classes can be not written with parentheses, does not inherit classes from other classes, and automatically inherits the built-in class object in the system
Pass empty statement, reserved statement position waiting to be completed later
Using classes
Form:
Class name ()
Cases:
Class Myclass:
"MyClass help."
Myclass=myclass () #实例化类
Print (' Output class Description: ')
Print (MYCLASS._DOC_)
Print (' Show class help info: ')
Help (MyClass)
The definition and invocation of a method of a class
Define the method:
(1), the first parameter of the method must be self, and cannot be omitted
(2), the method of the overall indentation of a unit, indicating that it belongs to the content of the class body
Call Method:
Call this class method: Self. Method name (argument list), do not provide self argument
Call the parent class method:
Form one: Parent class name. Method Name (Self,args)
Form two: Super (subclass name, self). Method Name (args)
Form three: Super (). Method Name (args)
Call methods in other classes:
(1), instantiation class
(2), instance name. Method Name (parameter list)
(3), do not provide self parameter
Construction Method _init_ (): Initialize related data when instantiating
Method Classification:
Instance method
Class method: Using the @classmethod adornment, you must have the default parameter "CLS", cannot use instance properties, class name invocation, or instance invocation
Static methods: Use @staticmethod adornments, no default parameters, cannot use instance properties, class name calls, or instance calls
Properties of the class:
Define a property in Python: use it directly. You can define properties in a constructor method, or you can define properties in other methods
Two types of properties:
Instance properties: Self. Property names different properties of the same class whose values are not associated with a reference: Self. Property name Scope: Entire class
Class Property: A reference that is common to all instances of a class with a property name: Class name. class variable Name
Specification of the use of underscores:
_xxx cannot be imported with "from module import", only the class and its subclasses can ask a member similar to protected decorated in Java
__xxx___ name of the system definition
The private variable in the __xxx class, only the class can access members similar to the private decoration in Java
Inheritance of Classes
Properties and methods with parent class: Inherit parent class (cannot inherit private property of parent class and Private method (property name or method name prefix is two underscore))
To implement a different behavior or capability from the parent class: Modify the Parent class's methods by overriding (The parameter list is consistent)
Multiple inheritance
Python allows multiple inheritance
Form: In the brackets that inherit the parent class, separate the parent class for multiple inheritance with ","
In multiple inheritance, the order of inheritance is an important factor: if you have inherited multiple parent classes that have the same method name, but do not specify a parent class name when used in a class, from left to right, a method with the same name in the first inherited class is called
Inheritance of classes and classes (6)