Rookie said inheritance in Python

xiaoxiao2021-03-06  37

Python supports object-oriented programming style, here is mainly to say more inheritance in Python:

The following code uses python2.4, after installing the IDE development environment (saying is IDE, more simple than Delphi, vs.net)

Establish a .py file from the FILE-New menu, write the following code:

Class Superclass:

DEF SAMPLE (Self):

Print 'SuperClass'

Class Subclass (SuperClass):

PASS

Sub = SUBCLASS ()

Sub.SAMPLE ()

To save first, press F5 to execute, display: in the main window of the iDLE:

>>> ================================ r= =============================================================================================================================================================================================================== ==================

>>>

Superclass

>>>

Subclasses call the Sample method of the parent class, now modify the code, as follows:

Class Superclass:

DEF SAMPLE (Self):

Print 'SuperClass'

Class Superclass1:

DEF SAMPLE (Self):

Print 'SuperClass1'

Class Subclass (Superclass, Superclass1):

PASS

Sub = SUBCLASS ()

Sub.SAMPLE ()

Run the code, what is the result of the same, the same, the first parent class of the subclass call, the second parent class is not called,

Now I know what to say below, change the statement of the Subclass class:

Class Subclass (Superclass1, SuperClass):

PASS

Run, the result of seeing is the SAMPLE method of superclass1 is called.

>>> ================================ r= =============================================================================================================================================================================================================== ==================

>>>

Superclass1

>>>

Here, it can be seen that in the case of multi-inheritance, the same method in the parent class will call the first parent class of the class declaration in the subclass.

转载请注明原文地址:https://www.9cbs.com/read-52077.html

New Post(0)