TypeError:__ init __()需要3个位置参数,但是给出了4个

时间:2017-09-25 23:41:26

标签: python

这些是我班级的代码:

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

class Salary:
    def jump(self, name, salary):
        print(self.name, self.salary)


class Male(Salary, Employee):
    def __init__(self, name, gender, occupation):
        super(Male, self).__init__(name, gender, occupation)
        self.occupation = occupation

# Separate from all classes (list of instantiated objects)
employee1 = Male("Jim", "male", "technician")
print(Male.name)

当我在创建所有类后使用代码的最后两行时,' TypeError: init ()需要3个位置参数,但是4个被给出'引用超级(男性,自我)......和employee1 =男性(......行。

3 个答案:

答案 0 :(得分:4)

在Pet下你有:

winapi.dll

在Dog下你有:

def __init__(self, name, color):
    self.name = name
    self.color = color

在Dog下,会给出一个额外的所有者位置参数,这会导致此错误。另外,我认为{3}在{3}中也可以正常工作

答案 1 :(得分:1)

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

class Salary:
    def __init__(self,name,gender):
        self.name=name
        self.gender=gender
    def jump(self):
        print(self.name,self.salary)

class Male(Salary,Employee):
    def __init__(self,name,gender,occupation):
        self.occupation=occupation
        super().__init__(name,gender)


employee1 = Male("Jim","male","technician")
print(employee1.name)

“”“您正在超级中编写职业参数,但是对父类的超级调用和您的父类没有职业参数。该职业仅属于Class Male。”“”

答案 2 :(得分:-1)

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

class Salary:
  def __init__(self, name, gender):
    self.name = name
    self.gender = gender   
  def jump(self):
    print(self.name, self.salary)

class Male(Salary, Employee):
    def __init__(self, name, gender, occupation):
      self.occupation = occupation
      Employee.__init__(self, name, gender)
      Salary.__init__(self, name, gender)

employee1 = Male("Jim", "male", "technician")
print(employee1.name)

Python - Inheritance

要访问基类Employee的所有方法和属性,派生类super()中使用了Male函数。 示例中显示了使用super函数的语法。 与此同时,请使用print(employee1.name)而不是print(Male.name)