Python object-oriented 2: inheritance, polymorphism, python Polymorphism

Source: Internet
Author: User

Python object-oriented 2: inheritance, polymorphism, python Polymorphism

The second feature of object-oriented is inheritance.

The methods of multiple classes can be extracted to the parent class, And the subclass only needs to inherit the parent class;

The basic syntax is the new class name of the class (parent class 1, parent class 2 ,..)

Differences between single inheritance and multi-inheritance:

Python also supports single inheritance and multi-inheritance. If there is only one parent class, it is single inheritance. If there are multiple parent classes, it is multi-inheritance.

Inherited features:

Subclass inherits all attributes and methods of the parent class, and can overwrite the variables and methods of the parent class with the same name.

Call method execution sequence: (see the case)

When calling a method, you must first call the method of the subclass. The subclass does not exist, and then search for the method in the parent class. The search order is from left (top down) to right.

If multiple parent classes have no parent classes, execute them from left to top and then to right.

If a common parent class exists, it is executed from left to the second-to-last layer, right, and finally to the top-level.



Case 1:

F2 inherits F1: similar to writing functions in F1 again in F2. If there is a method F2 that does not exist, go to F1 of the parent class to find it.

Class F1: # parent class, base class
Def show (self ):
Print ('show ')
Def foo (self ):
Print (self. name)
Class F2 (F1): # reference the F1 method to F2 again; subclass and derived class
Def _ init _ (self, name ):
Self. name = name
Def bar (self ):
Print ('bar ')
Def show (self ):
Print ('f2. show ')
Obj = F2 ('hh ')
Print ('----- foo method execution result --------')
Obj. foo () # execution result: hh, which needs to be found in the parent class
Print ('----- show method execution result --------')
Obj. show () # execute the result F2.show, first execute your own method, and then execute the method of the parent class

Execution result:

Case 2:If the parent class and the Child class have the same method, the Child class's own method is executed first. If the child class does not exist

# Self always represents the object itself
# If a method exists on its own, execute its own method. If it does not exist on its own, go to the parent class to find
Class S1:
Def F1 (self ):
Self. F2 ()
Def F2 (self ):
Print ('s1. f2 ')
Class S2 (S1 ):
Def F3 (self ):
Self. F1 ()
Def F2 (self ):
Print ('s2. f2 ')

Print ('---- S1.F2 -------------')
# Instantiate s1
Obj1 = S1 ()
Obj1.F1 () # first run F1 in S1 and then check if F2 exists in S1. If yes, run your F2
Print ('---- S2.F3 -------------')
# Instantiate s2
Obj2 = S2 ()
#1) Run F3 in s2 first. Because s2 does not have F1, F1 is executed in S1, and F2 needs to be executed in F1.
#2) self indicates s2. Because s2 has F2, execute F2 of S2.
Obj2.F3 ()

Execution result:

Case 3:Multi-inheritance, no common parent class, execution sequence: From left (first up) to right.

The Inheritance and execution sequence of left and right classes without the cap of common parent classes:

 
# First find the top layer on the left, top left, and top left, and then find the class on the right
Class c0:
Def f2 (self ):
Print ('c0. f2 ')
Class c1 (c0 ):
Def f1 (self ):
Print ('c1. f1 ')
Class c2:
Def f2 (self ):
Print ('c2. f2 ')
Def f4 (self ):
Print ('c2. f1 ')
Class c3 (c1, c2 ):
Def f3 (self ):
Pass
Obj = c3 () # instantiate a c3 object
Print ('------ c3.f2 -------')
Obj. f2 () # search for c3, c3 without f2, c1 without f2, and c0
Print ('------ c3.f1 -------')
Obj. f1 () # Go to c3 first, c3 without f1, and c1 again
Print ('------ c3.f4 -------')
Obj. f4 () # Go to c3 first, c3 without f4, c1 without f4, then go to c0, c0 without f4, and c2 again

Execution result:

Case 4:Multi-inheritance with a common parent class. Execute to the second-to-last layer, and then execute to the right.

# Locate the second-to-second stop, find it later, and finally find the same

Class c0:
Def f4 (self ):
Print ('c0. f2 ')
Class c_2 (c0 ):
Def f2 (self ):
Print ('C _ 2. f2 ')
Class c21 (c_2 ):
Def f1 (self ):
Print ('c21. f1 ')
Class c_1 (c0 ):
Def f1 (self ):
Print ('C _ 1. f2 ')
Class c11 (c_1 ):
Def f1 (self ):
Print ('c11. f1 ')
Class c3 (c11, c21 ):
Def f3 (self ):
Print ('c3. f3 ')
# Instantiate an object
Obj = c3 ()
Obj. f3 () # search for c3 in sequence and run the c3.f3 command.
Obj. f1 () # search for c3 and c11 in sequence, and run the c11.f1 command.
Obj. f2 () # search for c3, c11, c_1, c21, c_2 in sequence, and find the execution result c_2.f2.
Obj. f4 () # search for c3, c11, c_1, c21, c_2, c0 in sequence. The execution result is c0.f2.

Execution sequence:


The third feature of object-oriented architecture is polymorphism:It indicates that the parameter has multiple forms and forms, meaning that even if you do not know what the object class referenced by the variable is, you can still operate on it, it also shows different behaviors based on different object (or class) types.

Other programming languages, such as java, need to specify the parameter type when defining parameters, and must meet the type requirements when passing in parameters. But python basically does not care about this feature, because the type can be left unspecified when defining a variable in python. In the following example, different parameter types can be input, such as Dictionary, list, number, and string.

Example:

def func(arg):
print(arg)

func(1)
func("hh")
func(["a","b",6])
func({"a":1,"c":2})

Case link: https://pan.baidu.com/s/1skU53lV password: ifah

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.