简体   繁体   English

子类不继承父类

[英]Subclass not inheriting parent class

I'm having trouble with my code. 我的代码遇到了问题。 I'm trying to create a subclass which inherits the parent class's attributes and methods but it doesn't work. 我正在尝试创建一个继承父类的属性和方法的子类,但它不起作用。 Here's what I have so far: 这是我到目前为止所拥有的:

class Employee(object): 
  def __init__(self, emp, name, seat):
    self.emp = emp
    self.name = name
    self.seat = seat

Something is wrong with the block of code below - the subclass. 下面的代码块有些问题 - 子类。

Do I have to create the __init__ again? 我是否必须再次创建__init__ And how do I create a new attribute for the subclass. 我如何为子类创建一个新属性。 From reading questions, it sounds like __init__ in the subclass will override the parent class - is that true if I call it to define another attribute? 从阅读问题来看,听起来子类中的__init__会覆盖父类 - 如果我调用它来定义另一个属性,那是真的吗?

class Manager(Employee): 
  def __init__(self, reports):
    self.reports = reports
    reports = [] 
    reports.append(self.name) #getting an error that name isn't an attribute. Why? 

  def totalreports(self):
    return reports

I want the names from the Employee class to be in the reports list. 我希望Employee类中的名称位于报告列表中。

For example, if I have: 例如,如果我有:

emp_1 = Employee('345', 'Big Bird', '22 A')
emp_2 = Employee('234', 'Bert Ernie', '21 B')

mgr_3 = Manager('212', 'Count Dracula', '10 C')

print mgr_3.totalreports()

I want reports = ['Big Bird', 'Bert Ernie'] but it doesn't work 我想reports = ['Big Bird', 'Bert Ernie']但它不起作用

You never called the parent class's __init__ function, which is where those attributes are defined: 您从未调用父类的__init__函数,这是定义这些属性的位置:

class Manager(Employee): 
  def __init__(self, reports):
    super(Manager, self).__init__()
    self.reports = reports

To do this, you'd have to modify the Employee class's __init__ function and give the parameters default values: 为此,您必须修改Employee类的__init__函数并为参数提供默认值:

class Employee(object): 
  def __init__(self, emp=None, name=None, seat=None):
    self.emp = emp
    self.name = name
    self.seat = seat

Also, this code will not work at all: 此外,此代码根本不起作用:

  def totalreports(self):
    return reports

reports 's scope is only within the __init__ function, so it will be undefined. reports的范围仅在__init__函数内,因此它将是未定义的。 You'd have to use self.reports instead of reports . 您必须使用self.reports而不是reports

As for your final question, your structure won't really allow you to do this nicely. 至于你的最后一个问题,你的结构不会真正让你做得很好。 I would create a third class to handle employees and managers: 我会创建第三个类来处理员工和经理:

class Business(object):
  def __init__(self, name):
    self.name = name
    self.employees = []
    self.managers = []

  def employee_names(self);
    return [employee.name for employee in self.employees]

You'd have to add employees to the business by appending them to the appropriate list objects. 您必须通过将员工附加到相应的列表对象来将员工添加到业务中。

You need to run the superclass's init () in the appropriate place, plus capture the (unknown to the subclass) arguments and pass them up: 您需要在适当的位置运行超类的init (),并捕获(子类未知)参数并将其传递:

class Manager(Employee): 
  def __init__(self, reports, *args, **kwargs):
    self.reports = reports
    reports = [] 
    super(Manager, self).__init__(*args, **kwargs)
    reports.append(self.name) #getting an error that name isn't an attribute. Why? 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM