The concept of classes in Python programming can be likened to a description of a set of types, such as "humans" can be viewed as a class, and then the human class defines each specific person-you, me, him, etc. as its object. A class also has attributes and functions, attributes that are attributes of the class itself, such as human names, height, and weight, while the specific values vary according to each person, and function is what the class can do, such as human beings with functions such as eating, walking and sleeping. The specific form is as follows:
code is as follows |
copy code |
# Example: Class concept class humans: name = ' unnamed ' # member variable def talk (content): # member function Print Content # member variable assignment initial value someone = Human () # define a Human object someone SB. Name = "Passerby a" sb. Speak (' everyone good ') # passerby speak >>> Hello! # output |
A class is defined and used in Python in the form of: Class name [(parent class name)]:[member function and member variable], class name is the name of this class, and the parent class name is optional, but after the parent class name is defined, the subclass has the corresponding properties and methods of the parent class. When a class is defined as an object, the __init__ constructor is called first to initialize the properties of the object, and the properties of the class (member variables) can be defined in the constructor, as long as the object pointer is added to the definition. When an object is destroyed, the __del__ destructor is invoked, and when the member function of the class is defined, a variable (similar to the this pointer in C + +) must be used to represent the object itself defined by the class, and the name of the variable can be defined by itself.
The code is as follows |
Copy Code |
# Example: class definition and use Class Canimal: name = ' Unname ' # member variable def __init__ (self,voice= ' Hello '): # overloaded constructors Self.voice = Voice # Create member variables and assign initial values def __del__ (self): # overloaded destructor Pass # Empty operation def Say (self): Print Self.voice
t = canimal () # defines animal object T T.say () # t talk >> Hello # output Dog = Canimal (' wow ') # define Animal Objects Dog Dog. Say () # Dog Talk >> Wow # Output
|
In Python programming, classes can inherit the parent class attribute, the class name (the parent class), the subclass can inherit all the methods and properties of the parent class, or the member functions and properties of the parent class, and note that the subclass member function uses the subclass member function if it overloads the parent class (that is, the same name)
The code is as follows |
Copy Code |
# example: Inheritance of Classes Class Canimal: def __init__ (self,voice= ' Hello '): # Voice initialization defaults to Hello Self.voice = Voice def Say (self): Print Self.voice def Run (self): Pass # Empty Action statement (no action done)
Class Cdog (Canimal): # Inheriting class Canimal def setvoice (Self,voice): # Subclass Add Function Setvoice Self.voice = Voice def Run (Self,voice): # Subclass overloaded Function Run print ' Running '
Bobo = Cdog () Bobo. Setvoice (' My Name is bobo! ') # set Child.data to Hello Bobo. Say () Bobo. Run ()
>> my Name is bobo! >> Running |