class Person: def __init__(self,name,gender):#2 self.name=name self.gender=gender class Student(Person): def __init__(self,name,gender,score):#2+1 super(Student, self).__init__(name,gender) self.score=score class Teacher(Person): def __init__(self,name,gender,subject):#2+1 super(Teacher, self).__init__(name, gender) self.subject=subject class SkillMixin: def __init__(self,skill):#1 self.skill=skill class BasketballMixin(SkillMixin): def __init__(self,skill,basketball):#1+1 super(BasketballMixin, self).__init__(skill) self.basketball=basketball class FootballMixin(SkillMixin): def __init__(self,skill,football):#1+1 super(FootballMixin, self).__init__(skill) self.football=football class BasStudent(Student,BasketballMixin): def __init__(self,name,gender,score,skill,basketball):#3+2 super(BasStudent, self).__init__(name,gender,score) super(BasStudent, self).__init__(skill,basketball) print("我叫 %s,我会打%s "%(self.name,self.basketball)) class FootballTeacher(Teacher,FootballMixin): def __init__(self,name,gender,subject,skill,football): super(FootballTeacher, self).__init__(name,gender,subject) super(FootballTeacher, self).__init__(skill,football) print("我叫 %s,我会打%s "%(self.name,self.football)) a=Student('jiji','male',93) b=Teacher('kiki','female','English') c=BasketballMixin('skill','篮球') d=FootballMixin('ski','足球') BasStudent(a.name,a.gender,a.score,c.skill,c.basketball) FootballTeacher(b.name,b.gender,b.subject,d.skill,d.football)
Traceback (most recent call last):
File "/Users/lihui/IdeaProjects/laern/Python j进阶/3章 类的继承.py", line 278, in <module>
BasStudent(a.name,a.gender,a.score,c.skill,c.basketball)
File "/Users/lihui/IdeaProjects/laern/Python j进阶/3章 类的继承.py", line 266, in __init__
super(BasStudent, self).__init__(skill,basketball)
TypeError: __init__() missing 1 required positional argument: 'score'
你在BasStudent里面两次调用了super方法,一厢情愿地认为会分别调用两个父类的init方法,但实际上不是,所以程序报错认为缺参数(可能是两次调用了同一个三参数的父类init方法)。具体原因我也没搞清楚,不过网上的忠告:不惜一切代价地避免多重继承,它带来的麻烦比能解决的问题都多。如果你非要用,那你得准备好专研类的层次结构,以及花时间去找各种东西的来龙去脉吧!
score需要必填的呀