Violet2forever
2021-04-21 07:53
采纳率: 100%
浏览 47
已采纳

python3中为什么我的info没有从父类中继承到子类?

报错如下

Traceback (most recent call last):
  File "E:/pythonProject3/8/8.1.py", line 23, in <module>
    stu = Student('张三', 20, '10001')
  File "E:/pythonProject3/8/8.1.py", line 14, in __init__
    super().__init__(name, age)
TypeError: object.__init__() takes exactly one argument (the instance to initialize)

代码如下

class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def info(self):
        print(self.name, self.age)


class Student:
    def __init__(self, name, age, stu_no):
        super().__init__(name, age)
        self.stu_no = stu_no

class Teacher:
    def __init__(self, name, age, year):
        super().__init__(name, age)
        self.year = year


stu = Student('张三', 20, '10001')
teacher = Teacher('李四', 34, 10)
stu.info()

在输入stu.info的时候显示没有这个类,不知道是因为什么原因导致没有继承到子类

4条回答 默认 最新

相关推荐 更多相似问题