super().__ repr __()没有返回预期的结果

时间:2017-11-23 05:07:22

标签: python class init super repr

class Person:
    def __init__(self, nameInput, addressInput, dateOfBirthInput):
        self._name = nameInput
        self._address = addressInput
        self._dateOfBirth = dateOfBirthInput
    def getName(self):
        return self._name
    def getAddress(self):
        return self._address
    def getDateOfBirth(self):
        return self._dateOfBirth
    def __repr__(self):
        return 'Name: %s \nAddress: %s \nDate of Birth: %s' % (self._name, self._address, self._dateOfBirth)

如果我输入类似kat = Person('CC','9th Street','1990')的内容,第一堂课将正确输出。但是当我尝试从子类中调用它时,我无法正常工作,它只是将所有内容留空。

from Person import Person

class Employee(Person):
    def __init__(self, nameInput, addressInput, dateOfBirthInput, employeeNumberInput, dateOfHireInput):
        super().__init__(nameInput, addressInput, dateOfBirthInput)
        self._employeeNumber = employeeNumberInput
        self._dateOfHire = dateOfHireInput
    def getEmployeeNumber(self):
        return self._employeeNumber
    def getDateOfHire(self):
        return self._dateOfHire
    def __repr__(self):
        return super().__repr__() + '\nEmployee Number: %s \nDate Of Hire: %s' % (self._employeeNumber, self._dateOfHire)

如果我要在上面的块中输入类似kat = Employee('CC','9th Street','1990','12345','2017')的内容,我什么也得不到,我错过了一些明显的东西,我已经用一些不同的方式重写它以匹配示例但是我的方式和示例都不适用我

2 个答案:

答案 0 :(得分:0)

所以经过一些摆弄后,似乎根本没有任何错误,无论是否运行似乎都是由我运行它决定的。我在Visual Studio中编写它并没有在那里工作,但是当我在空闲时将它推出它时工作正常。

答案 1 :(得分:-1)

在您的Employee类中执行此操作

class Employee(Person):
...:     def __init__(self, nameInput, addressInput, dateOfBirthInput, employeeNumberInput, dateOfHireInput):
...:         super(Employee, self).__init__(nameInput, addressInput, dateOfBirthInput)
...:         self._employeeNumber = employeeNumberInput
...:         self._dateOfHire = dateOfHireInput
...:     def getEmployeeNumber(self):
...:         return self._employeeNumber
...:     def getDateOfHire(self):
...:         return self._dateOfHire
...:     def __repr__(self):
...:         return super(Employee, self).__repr__() + '\nEmployee Number: %s \nDate Of Hire: %s' % (self._employeeNumber, self._dateOfHire)

这适用于您的模块