With the old Ziko Python writing class of the three subclass _python

Source: Internet
Author: User

About the class, reader must have had the feeling, look at the following code, please read carefully, and see if you can find something wrong?

Copy Code code as follows:

#!/usr/bin/env python
#coding: Utf-8

Class Person:
def __init__ (self, name, Lang, email):
Self.name = Name
Self.lang = Lang
Self.email = Email

def author (self):
Return Self.name

Class Programmer:
def __init__ (self, name, Lang, email, system, website):
Self.name = Name
Self.lang = Lang
Self.email = Email
Self.system = System
Self.website = website

def pythoner (self):
Pythoner_list = [Self.name, Self.lang, Self.email, Self.system, Self.website]
Return pythoner_list

If __name__== "__main__":
writer = person ("Qiwsir", "Chinese", "qiwsir@gmail.com")
Python = programmer ("Qiwsir", "Python", "qiwsir@gmail.com", "Ubutun", "Qiwsir.github.io")
Print "My Name is:%s"%writer.author ()
Print "I Write program by:%s"%python.pythoner () [1]

The above code, the operation is not a problem, but, look carefully, found that there are two classes, a name is called person, another is called programmer, which is not the problem, the problem is that these two classes in the constructor, there is the same place: Self.name=name , Self.lang=lang,self.email=email, this is generally not allowed for programmers pursuing code quality. It is best not to have duplicate code or redundant code. However, in two classes have these parameters, what should be done?

Subclasses, parent classes, and inheritance

Look at the code below, which has two class a,b. This program works correctly, and the function of each class is simply to print the specified content.

Copy Code code as follows:

#!/usr/bin/env python
#coding: Utf-8

Class A:
def __init__ (self):
Print "AAA"

Class B:
def __init__ (self):
Print "BBB"

If __name__== "__main__":
A = A ()
b = B ()

#运行结果
Aaa
Bbb

The above two classes do not have a parent-child relationship with each other. Now a little change, the class B rewrite, pay attention to observe the difference with the above.

Copy Code code as follows:
#!/usr/bin/env python
#coding: Utf-8

Class A:
def __init__ (self):
Print "AAA"

Class B (A): #这里和上面程序不同. b inherits A.
def __init__ (self):
Print "BBB"

If __name__== "__main__":
A = A ()
b = B ()

#运行结果
Aaa
Bbb

In this program, Class B is a little different from the previous paragraph, Class B (A):, so it shows the relationship between B relative A: B is a subclass of a, b inherits everything from A (son).

However, reader found no, running the same result. Yes, that is to think that despite the succession of a in B, but did not call any a thing, it is like a son from his father inherited wealth, but the son did not move a child, the outside world see and no inheritance.

Copy Code code as follows:

#!/usr/bin/env python
#coding: Utf-8

Class A:
def __init__ (self):
Print "AAA"

Class B (A):
def __init__ (self):
#print "BBB."
A.__init__ (self) #运行继承的父类

If __name__== "__main__":
A = A ()
b = B ()

#运行结果
Aaa
Aaa

This time the result of the run has changed, originally B=b () is run Class B, but B inherits a, and in the initialization constructor, introduces a constructor, so the result of running a is the corresponding result.

The first end of the program is overridden by subclass inheritance, which can be as follows:

Copy Code code as follows:

#!/usr/bin/env python
#coding: Utf-8

Class Person:
def __init__ (self, name, Lang, email):
Self.name = Name
Self.lang = Lang
Self.email = Email

def author (self):
Return Self.name
"""
Class Programmer:
def __init__ (self, name, Lang, email, system, website):
Self.name = Name
Self.lang = Lang
Self.email = Email
Self.system = System
Self.website = website

def pythoner (self):
Pythoner_list = [Self.name, Self.lang, Self.email, Self.system, Self.website]
Return pythoner_list
"""

Class programmer (person): #继承父类Person
def __init__ (self, name, Lang, email, system, website):
person.__init__ (Self,name,lang,email) #将Person. __init__ () inherits the functionality from here.
#self. Name = Name #这三句是Person中已经搞定的, you do not have to repeat
#self. lang = lang #通过继承已经实现了这三句的功能
#self. email = Email
Self.system = System #子类中不同于Person父类部分
Self.website = website

def pythoner (self):
Pythoner_list = [Self.name, Self.lang, Self.email, Self.system, Self.website]
Return pythoner_list

If __name__== "__main__":
writer = person ("Qiwsir", "Chinese", "qiwsir@gmail.com")
Python = programmer ("Qiwsir", "Python", "qiwsir@gmail.com", "Ubutun", "Qiwsir.github.io")
Print "My Name is:%s"%writer.author ()
Print "I Write program by:%s"%python.pythoner () [1]

The code runs the same result as before.

Do you understand the subclass and the parent class, inheritance characteristics. If you have a father, is a senior official or rich, then you are the second generation of officials or rich second generation, you inherit a lot of wealth from them, so life will not be too tired. This is the role of inheritance. In code, similarly, inheritance can make writing code less tiring.

For why to use inheritance, friend @ Make the Fox heroes gave a very wonderful explanation:

Copy Code code as follows:

Technically, in OOP, the main purpose of inheritance is to achieve polymorphism. For polymorphic, it is not necessarily that the inheritance of interfaces, the inheritance of attributes and behaviors is important. In fact, a lot of engineering practice shows that heavy behavioral inheritance can cause the system to be overly complex and bloated, but it will reduce the flexibility. Therefore, the concept of mild succession based on interface is advocated. In this model, because the parent class (interface Class) has no code at all, there is no code reuse at all.

In Python, because of the existence of duck Type, the importance of interface definitions is greatly reduced, and the role of inheritance is further weakened.

In addition, logically, the purpose of inheritance is not to reuse code, but to straighten out relationships.


I express my full agreement with the above explanation. However, if reader does not understand, there is no relationship, the spirit of the above explanation, it really needs to be in the programming practice to understand.

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.