为了账号安全,请及时绑定邮箱和手机立即绑定

这个是出什么问题了?怎么改?求大神帮我看一下

https://img1.sycdn.imooc.com/65ebd0b2000184c413750660.jpg

想不明白,想不出来

正在回答

1 回答

在继承了两个方法的情况下,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())





https://img1.sycdn.imooc.com/660626b30001918b08770315.jpg

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

这个是出什么问题了?怎么改?求大神帮我看一下

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信