简体   繁体   中英

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? 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?

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.

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

You never called the parent class's __init__ function, which is where those attributes are defined:

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:

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. You'd have to use self.reports instead of 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:

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? 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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