The Python programming language is a powerful development language. its biggest feature is its ease of use. It also has object-oriented features, which can help us implement some specific functional requirements. We will introduce the concept of Python inheritance in detail here today. The Python programming language is a powerful development language. its biggest feature is its ease of use. It also has object-oriented features, which can help us implement some specific functional requirements. We will introduce the concept of Python inheritance in detail here today.
The following code uses Python2.4 and the idle IDE development environment after installation (IDE is much simpler than delphi and VS.net)
Create a. py File from the File-New menu and write down the following Python inheritance code:
>>>=============== RESTART ======================
>>> Class SuperClass: 1.def sample (self ):
Print 'superclass'
2. class SubClass (SuperClass ):
Pass
3. sub = SubClass ()
4. sub. sample ()
Save the file and press F5 to display it in the main idle window:
The subclass calls the sample method of the parent class. the code is modified as follows:
Class SuperClass: 1.def sample (self ):
Print 'superclass'
2. class SuperClass1:
Def sample (self ):
3. print 'superclass1'
Class SubClass (SuperClass, SuperClass1 ):
4. pass
Sub = SubClass ()
5. sub. sample ()
Run the Python inheritance code. The result is the same as the above. here, the subclass calls the sample method of the first parent class, and the second parent class is not called, now you know what to say. change the declaration of the SubClass class:
Class SubClass (SuperClass1, SuperClass): 1. pass
Run. The result is that the SuperClass1 sample method is called.
>>>=============== RESTART ====================== 1. >>>
SuperClass1
2. >>>
Here, we can see that when Python inherits, the same method in the parent class will call the method of the first parent class declared by the class in the subclass.
The above is the Python inheritance and object-oriented parsing content. For more information, please follow the PHP Chinese network (www.php1.cn )!