With the old Ziko Python's three sub-categories of writing class

Source: Internet
Author: User
About the class, crossing must already have a feeling, look at the code below, please read it carefully and see if you can find something wrong?
Copy CodeThe code is 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, run without any problem, but, look closely, found that there are two classes, a name called person, the other is called programmer, this is not the problem, the problem is that the two classes of the constructor, there is the same place: Self.name=name , Self.lang=lang,self.email=email, which is generally not allowed for programmers who pursue code quality. It is best not to have duplicate code or redundant code. However, you have to have these parameters in all two classes, what should I do?

Subclass, parent class, and inheritance

Look at the following code, there are two classes, a, B. This program works correctly, and the function of each class is to print only the specified content.
Copy the 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 so-called parent-child relationship. Now change slightly, rewrite class B, and observe the difference.
Copy the 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 procedure, Class B is a little different from the previous paragraph, Class B (A):, this indicates the relationship of B relative A: B is a subclass of a, b inherits everything from a (son Seung-so).

However, crossing found no and ran the same result. Yes, that is to think that in B although inherited a, but did not call anything a, like the son inherited wealth from his father, but the son did not move, the outside world saw and did not inherit the same.
Copy the 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 operation has changed, originally B=b () is running Class B, but B inherits a, and in the initialization of the constructor, a constructor is introduced, so the result of running a corresponding result.

The following is the beginning of the end of the program with the subclass inherited by the way of rewriting, can be this:
Copy the 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__ () function inherited here
#self. Name = Name #这三句是Person中已经搞定的, do not 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.

Does yours faithfully understand the characteristics of subclasses and parent classes, inheritance? If you have a father, is a high-ranking or rich, then you are two generations 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, too, inheritance can make writing code less tiring.

For why to use inheritance, friends @ Linghutao to give a very wonderful explanation:

Copy the Code code as follows:


Technically speaking, the main use of inheritance in OOP is to achieve polymorphism. For polymorphism, it is important that interface inheritance, property and behavior have inheritance, which is not necessarily. In fact, a large number of engineering practices have shown that heavy behavioral inheritance can lead to a system that is overly complex and bloated, instead reducing flexibility. Therefore, the concept of light inheritance based on interface is now advocated. In this model, because the parent class (The interface Class) has no code at all, there is no code reuse at all.

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

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


I express my full support for the above explanations. But crossing if not understand, also has no relationship, the spirit of the above explanation, really need in the programming practice to comprehend to understand.
  • 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.