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

报错了,请大神们帮我看看哪里错了呗!

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'


正在回答

2 回答

你在BasStudent里面两次调用了super方法,一厢情愿地认为会分别调用两个父类的init方法,但实际上不是,所以程序报错认为缺参数(可能是两次调用了同一个三参数的父类init方法)。具体原因我也没搞清楚,不过网上的忠告:不惜一切代价地避免多重继承,它带来的麻烦比能解决的问题都多。如果你非要用,那你得准备好专研类的层次结构,以及花时间去找各种东西的来龙去脉吧!

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

高飞的鱼 提问者

好,我试试填写上score,可能我以为调用了,其实没有调用成功
2022-04-06 回复 有任何疑惑可以回复我~

score需要必填的呀

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

高飞的鱼 提问者

self.score=score吗,我以为调用了super,就不用了
2022-04-06 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

报错了,请大神们帮我看看哪里错了呗!

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