问答详情
源自:3-4 Python中的多态

多重继承的定义格式不是子类写全父类的参数,再另外调用吗,为什么报错位置参数

class Person(object):
    def __init__(self,name):
        self.name=name
    def who(self):
        return 'I am a Person,my name is %s'%self.name
    
class Student(Person):
    def __init__(self,name,score):
        super(Student,self).__init__(name)
        self.score=score
    def who(self):
        return 'I am a Student,my name is %s'%self.name
        
class Teacher(Person):
    def __init__(self,name,course):
        super(Teacher,self).__init__(name)
        self.course =course
    def who(self):
        return 'I am a Teacher,my name is %s'%self.name
        
class Work(Student,Teacher):
    def __init__(self,name,score,course,timetable):
        super(Work,self).__init__(name,score,course)
        self.timetable=timetable
p = Person('Tim')
s = Student('Bob',12, )
t = Teacher('Alice',  'English')
w=Work('Herman',96,'english',22)
print(w.who())


提问者:hermaniu 2021-11-23 10:00

个回答

  • hermaniu
    2021-11-23 10:00:21

    Traceback (most recent call last):

      File "C:\Users\hermaniu\Desktop\test1.py", line 162, in <module>

        w=Work('Herman',96,'english',22)

      File "C:\Users\hermaniu\Desktop\test1.py", line 156, in __init__

        super(Work,self).__init__(name,score,course)

    TypeError: Student.__init__() takes 3 positional arguments but 4 were given