Let's talk about inheritance after learning the compiling of Python.

Source: Internet
Author: User

Let's talk about inheritance after learning the compiling of Python.

On the basis of the previous Code lecture, we made further modifications to become the following program. Please read this program:
Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8

Class Person:
Def _ init _ (self, name, email ):
Self. name = name
Self. email = email

Class Programmer (Person ):
Def _ init _ (self, name, email, lang, system, website ):
Person. _ init _ (self, name, email)
Self. lang = lang
Self. system = system
Self. website = website

Class Pythoner (Programmer ):
Def _ init _ (self, name, email ):
Programmer. _ init _ (self, name, email, "python", "Ubuntu", "qiwsir. github. io ")

If _ name __= = "_ main __":
Writer = Pythoner ("qiwsir", "qiwsir@gmail.com ")
Print "name =", writer. name
Print "lang =", writer. lang
Print "email =", writer. email
Print "system =", writer. system
Print "website =", writer. website

# Running result

Name = qiwsir
Lang = python
Email = qiwsir@gmail.com
System = Ubuntu
Website = qiwsir. github. io

Satisfied with the results, let's look at the inheritance relationship in the program: Pythoner <-- Programmer <-- Person. From the process above, it is not difficult to see that inheritance can reduce code duplication, but the code is more concise. In addition, you can assign values to parameters by default during inheritance.

In order to highlight the exploration of inheritance problems, we still use the simple class to do experiments.

Redundant B
Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8

Class:
Def _ init _ (self ):
Print "aaa"

Class B ():
Pass

If _ name __= = "_ main __":
A = ()
B = B ()

# Running result

Aaa
Aaa

B inherits from A without any modification. B does not need to write anything, or B is essentially redundant. In the real programming process, this is not written in this way. It is only intended to show the viewer the meaning of inheritance.
Copy codeThe Code is as follows:
# Valid first inheritance

#! /Usr/bin/env python
# Coding: UTF-8

Class:
Def _ init _ (self ):
Print "aaa"

Class B:
Def _ init _ (self ):
Print "bbb"

Class C1 (A, B ):
Pass

Class C2 (B, ):
Pass

If _ name __= = "_ main __":
Print "A ---> ",
A = ()
Print "B ---> ",
B = B ()
Print "C1 (A, B) ---> ",
C1 = C1 ()
Print "C2 (B, A) ---> ",
C2 = C2 ()

# Running result

A ---> aaa
B ---> bbb
C1 (A, B) ---> aaa
C2 (B, A) ---> bbb

Check whether the column is noticed. Class C1 inherits two classes A and B. Class C2 also inherits two classes, but the writing order is A little different (B, ). From the running results, we can see that when the subclass inherits multiple parent classes, only the first constructor _ init _ () can be inherited, and the second class will be equal. Therefore, in general, there will not be multiple constructor inheritance in the program at the same time, but the inheritance can be relayed, just like the previous more real code.

Inheritance of other methods
Copy codeThe Code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8

Class:
Def _ init _ (self ):
Print "aaa"
Def amethod (self ):
Print "method"

Class B ():
Def _ init _ (self ):
Print "bbb"


If _ name __= = "_ main __":
Print "A --->"
A = ()
A. amethod ()
Print "B --->"
B = B ()
B. amethod ()

# Running result

A --->
Aaa
Method
B --->
Bbb
Method

I drew a picture to illustrate the above situation. However, after I finished the painting, I regretted it. It seems more confused to see this picture. I drew some pictures and posted them. It would be better if I could help to understand them.

A's instance and call. Focusing on B, Class B inherits A. At the same time, B makes its own provisions in the constructor, that is, B's constructor is executed according to B's will, do not execute the content of A. However, A also has an amethod (self) method, and B inherits this method. When this method is called through an instance of Class B, it will be successful: B. amethod ()

This is the method inheritance and calling method.

Inheritance is to find the corresponding inheritance objects from the bottom to the upper level. What if there is a name? In what order?

Application online section:

In Python, you can perform multiple inheritance operations. At this time, pay attention to the sequence of search operations, starting from the subcategories, next, the parent class of the same region should not be searched from left to right, and then the parent class of the same region should not be searched from left to right until the parent class ends.

Code example:
Copy codeThe Code is as follows:
Class A (object ):
Def method1 (self ):
Print ('a. method1 ')

Def method2 (self ):
Print ('a. method2 ')

Class B ():
Def method3 (self ):
Print ('B. method3 ')

Class C ():
Def method2 (self ):
Print ('C. method2 ')

Def method3 (self ):
Print ('C. method3 ')

Class D (B, C ):
Def method4 (self ):
Print ('C. method4 ')

D = D ()
D. method4 () # Find in D, C. method4
D. method3 () # locate in sequence D-> B, B. method3
D. method2 () # in the order D-> B-> C sequence, C. method2
D. method1 () # locate in sequence D-> B-> C-> A. method1

Make sure that the real learners can find the corresponding output results in sequence based on each method of each class. To understand the inheritance sequence. Learning requires accumulation.




Related Article

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.