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

来源:3-4 Python中的多态

高飞的鱼

2022-03-05 09:06

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回答

  • weixin_慕圣3493772
    2022-04-06 11:12:09

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

    高飞的鱼

    好,我试试填写上score,可能我以为调用了,其实没有调用成功

    2022-04-06 11:16:40

    共 1 条回复 >

  • 慕梦前来
    2022-03-31 18:54:49

    score需要必填的呀

    高飞的鱼

    self.score=score吗,我以为调用了super,就不用了

    2022-04-06 11:16:01

    共 1 条回复 >

Python3 进阶教程(新版)

学习函数式、模块和面向对象编程,掌握Python高级程序设计

41910 学习 · 236 问题

查看课程

相似问题