想不明白,想不出来
在继承了两个方法的情况下,super()只会调用第一个函数的__init__ 方法,而不会调用第二个函数,所以会出现传参多的问题。修改一下代码,例如:
# Enter a code
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Student(Person):
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score
class Teacher(Person):
def __init__(self, name, gender, score):
super(Teacher, self).__init__(name, gender)
self.score = score
class SkillMixin(object):
def __init__(self, sports):
self.sports = sports
class BasketbaMixin(SkillMixin):
def __init__(self, sports):
super(BasketbaMixin, self).__init__(sports)
class FootballMixin(SkillMixin):
def __init__(self, sports):
super(FootballMixin, self).__init__(sports)
class PlayBasketballStudent(BasketbaMixin, Student):
def __init__(self, name, gender, score, sports):
Student.__init__(self, name, gender, score)
BasketbaMixin.__init__(self, sports)
def get_info(self):
return 'I am a Student, my name is %s, I am a %s, I like %s, my score is %s' % (self.name, self.gender, self.sports, self.score)
class PlayFootballTeacher(Teacher, FootballMixin):
def __init__(self, name, gender, score, sports):
Teacher.__init__(self, name, gender, score)
FootballMixin.__init__(self, sports)
def get_info(self):
return 'I am a Teacher, my name is %s, my score is %s, I like %s' % (self.name, self.score, self.sports)
student = PlayBasketballStudent("Alice", "female", "90", "basketball")
teacher = PlayFootballTeacher("Bob", "male", "30", "football")
print(teacher.get_info())
print(student.get_info())