Python 3 OOP-“ super().__ init __()”到底在哪里?

时间:2019-11-13 06:01:29

标签: python python-3.x oop inheritance

我正在尝试使我的子类工作,但是继承有问题。传入的args数应为5:姓名,年龄,性别,头衔和薪水

但是,Python在说

TypeError: __init__() takes 4 positional arguments but 6 were given

,我不知道为什么或如何解决。这是我的代码:

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

class Employee(Person):
    def emp_function(self, title, salary):
        self.title = title
        self.salary = salary
        super().__init__()   
     #Is this wrong? Where should this 'super()' go?

George = Employee("George", 30, "male", "Manager", 50000)

1 个答案:

答案 0 :(得分:1)

super().__init__()应该在Employee类的def __init__

您还需要为Employee类创建构造函数。

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

class Employee(Person):
    def __init__(self,name,age,gender,title,salary):
        self.title = title
        self.salary = salary
        super().__init__(name, age, gender)   


George = Employee("George", 30, "male", "Manager", 50000)