The Python programming language is a powerful development language, and its greatest feature is its ease of use. But also has the object-oriented characteristic, can help us to realize some specific function requirement very well. We will be here today to give you a detailed description of the concepts related to Python inheritance.
The following code uses Python2.4, using the Idle IDE development environment after installation (say IDE, much simpler than delphi,vs.net)
To create a. py file from the File-new menu, write 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 ()
To save first, then press F5 to execute, in the Idle main window display:
The subclass calls the sample method of the parent class and now modifies the code 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 python to inherit the code, see the result is the same as above, where the subclass called the first parent class of the sample method, the second parent class is not called, now know what to say, and then change the subclass class declaration to:
Class Subclass (Superclass1,superclass): 1.pass
Run, see the result is SuperClass1 the sample method is called.
>>> ============= RESTART =============== 1.>>>
SuperClass1
2.>>>
Here, it can be seen that in the case of Python inheritance, the same method in the parent class invokes the method of the first parent class declared by the class in the child class.
The above is the Python inheritance and object-oriented parsing content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!