Python self-taught music-Multiple inheritance sequence issues

Source: Internet
Author: User

Recently self-taught Python to object-oriented here, feel the need to take more than the inheritance here to understand, here is my own little summary, if a friend feel that there is not enough, but also look at the liberal enlighten!

1.
#Author: Clark

Class Animal (object): #动物类
#kind = ""
def __init__ (Self,name,age,food):
Self. Name = Name
Self. Age = Age
Self.food = Food
Def eat (self):
Print ('%s is eat%s '% (self. Name,self.food))
def barking (self):
Print ("%s is barking"%self. Name)
Class Relation (object): #动物关系类
def makefrined (self,obj):
Print ("%s is making friend with%s"% (self.) Name,obj. Name))

Class Dog (Relation,animal): #狗继承关系类与动物类
def __init__ (Self,name,age,food,kind):
Super (Dog,self) __init__ (Name,age,food)
Self.kind = Kind
def barking (self):
Print ("The barking of%s is wangwangwang!" %self. Name)
Class Cat (Animal):
def __init__ (Self,name,age,food,kind):
Super (Cat,self) __init__ (Name,age,food)
Self.kind = Kind
def barking (self):
Print ("The barking of%s is Miaomiaomiao"%self. Name)

Dog_teddy = Dog ("Ahuang", 3, "Bone", "Teddy")
Cat_xiaohua = Cat ("Xiaohua", 2, "Fish", "cat")
Dog_teddy.makefrined (Cat_xiaohua)
The result of the operation is:
Ahuang is making friend with Xiaohua
The first doubts are, why the Makefriend method of the class relation, obj.name from where, how to run successfully?
Later found in the Python class inheritance, there is a sequence: the first implementation is its own construction method, where the dog class has its own method of construction, and defines and inherits the name of the animal class, and so on, so it will execute successfully;
And then I thought, what if we get rid of the dog's own method of construction? Show me the Code
2.
#Author: Clark
Class Animal (object): #动物类
#kind = ""
def __init__ (Self,name,age,food):
Self. Name = Name
Self. Age = Age
Self.food = Food
Def eat (self):
Print ('%s is eat%s '% (self. Name,self.food))
def barking (self):
Print ("%s is barking"%self. Name)
Class Relation (object): #动物关系类
def makefrined (self,obj):
Print ("%s is making friend with%s"% (self.) Name,obj. Name))

Class Dog (Relation,animal): #狗继承关系类与动物类
# def __init__ (Self,name,age,food):
# super (Dog,self) __init__ (Name,age,food)
# Self.kind = Kind
def barking (self):
Print ("The barking of%s is wangwangwang!" %self. Name)
Class Cat (Animal):
def __init__ (Self,name,age,food,kind):
Super (Cat,self) __init__ (Name,age,food)
Self.kind = Kind
def barking (self):
Print ("The barking of%s is Miaomiaomiao"%self. Name)

Dog_teddy = Dog ("Ahuang", 3, "bone")
Cat_xiaohua = Cat ("Xiaohua", 2, "Fish", "cat")
Dog_teddy.makefrined (Cat_xiaohua)
The result of the operation is:
Ahuang is making friend with Xiaohua
Huh? Why is it? Obviously dog does not have a method of construction, supposedly dog's instance object to execute the Makefriend method, found that the class relation does not have the name of the property, should be thrown wrong to ah?
Just found that the Python class in the inheritance of the time is to find the construction method, where the dog has no construction method, relation also did not, then find animal, found that, for granted, the implementation of success!
In fact, you may know that the Python3 class inherits the order of a left-to-right principle, here if the Dog class inheritance sequence written in dog (Animal,relation), may feel good to understand a little, actually from left to right is right, the principle is to find the structure
Method is no longer looking down, you can verify that show me the Code
3.
#Author: Clark
Class Animal (object): #动物类
#kind = ""
def __init__ (Self,name,age,food):
Self. Name = Name
Self. Age = Age
Self.food = Food
Def eat (self):
Print ('%s is eat%s '% (self. Name,self.food))
def barking (self):
Print ("%s is barking"%self. Name)
Class Relation (object): #动物关系类
def __init__ (self): #给Relation写一个构造方法
Pass
def makefrined (self,obj):
Print ("%s is making friend with%s"% (self.) Name,obj. Name))

Class Dog (Relation,animal): #这里的顺序注意了, the different order of two classes is differentiated
# def __init__ (Self,name,age,food):
# super (Dog,self) __init__ (Name,age,food)
# Self.kind = Kind
def barking (self):
Print ("The barking of%s is wangwangwang!" %self. Name)
Class Cat (Animal):
def __init__ (Self,name,age,food,kind):
Super (Cat,self) __init__ (Name,age,food)
Self.kind = Kind
def barking (self):
Print ("The barking of%s is Miaomiaomiao"%self. Name)

Dog_teddy = Dog ("Ahuang", 3, "bone")
Cat_xiaohua = Cat ("Xiaohua", 2, "Fish", "cat")
Dog_teddy.makefrined (Cat_xiaohua)
The result of the operation is error:
TypeError: __init__ () takes 1 positional argument but 4 were given
Because first find relation construction method, found no attributes, no longer look down, direct error! Can try animal write in front, will not error.

More than 1, 2, 3 in my own language to organize the following summary (for reference spit Groove):
The simple point is that Python's multiple inheritance has a sequence, its own construction method > The First Class construction method > is greater than the second class construction method, and so on, find the first construction method, no longer go down to find!!













Python self-taught music-Multiple inheritance sequence issues

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.