Python继承2.7构造函数不起作用

时间:2017-12-24 17:42:22

标签: python python-2.7 inheritance constructor

IDE PyCharm Python版本2.7

我一直在试图弄清楚如何让构造函数中的继承工作一段时间,但我仍然遇到问题。

我有一个超级人物:

class Person(object):

    def __init__(self, name, address, email, phoneNumber):
        self.name = name
        self.address = address
        self.email = email
        self.phoneNumber = phoneNumber

一个子类Worker,它位于项目中的另一个python文件中:

from Person import Person
class Worker(Person):

    def __init__(self, name, address, email, phoneNumber, title):
        super(Worker, self).__init__(name, address, email, phoneNumber)
        self.title = title

However, when I try to make a Worker like this:
w = Worker("Paul", "Adrees", "@", "#" "worker")

我收到错误: w =工人(“保罗”,“Adrees”,“@”,“#”“工人”) TypeError: init ()只需要6个参数(给定5个)

请帮助!

1 个答案:

答案 0 :(得分:0)

你忘记了","之间"#"和" worker",这就是为什么根据Worker.__init__签名在预期6时产生5个参数的原因。试试这个:

w = Worker("Paul", "Adrees", "@", "#", "worker")

对我有用!