-1

我正在上编程课,对于其中一个测试,我需要创建一个继承自已创建类的类。这是老师教我做的代码,但它似乎不起作用:

class Intern(Employer):
    def __init__(self, last_name, first_name, address, phone, email, end_date):
        Employer(last_name, first_name, address, phone, email)
        self.end_date=end_date
    def intern_info(self):
        self.print_info()
        print self.end_date
4

3 回答 3

6

线

Employer(last_name, first_name, address, phone, email)

创建一个Employer在执行__init__()结束时立即被垃圾回收的对象。它不会Intern以任何方式影响对象。你的意思可能是这样的

Employer.__init__(self, last_name, first_name, address, phone, email)

或许

super(Intern, self).__init__(last_name, first_name, address, phone, email)
于 2012-12-17T17:33:04.710 回答
2

selfEmployer. 您需要使用superself显式传递给父级的__init__. 您还需要显式调用父级的__init__方法。

class Intern(Employer):
    def __init__(self, last_name, first_name, address, phone, email, end_date):
        Employer.__init__(self, last_name, first_name, address, phone, email)
        self.end_date=end_date
    def intern_info(self):
        self.print_info()
        print self.end_date
于 2012-12-17T17:31:24.853 回答
0

您缺少缩进​​,这是 Python 中代码块的机制。

尝试:

class Intern(Employer):
    def __init__(self, last_name, first_name, address, phone, email, end_date):
        Employer(last_name, first_name, address, phone, email)
        self.end_date=end_date
    def intern_info(self):
        self.print_info()
        print self.end_date
于 2012-12-17T17:31:18.100 回答