您好我正在尝试编写一个方法,用于打印员工的位置列表,并将其报告给经理。创建管理器对象并保存向管理器报告的人员的ldaps(id)列表。
如何遍历所有员工对象 - 在这种情况下,已创建了3名员工?下面的GetLocations方法仅打印管理器位置。任何帮助,将不胜感激。谢谢!
我想有一个输出说:都柏林,都柏林纽约(格式无关)
class Employee(object):
def __init__(self, ldap, name, location, salary, status):
self.ldap = ldap
self.name = name
self.location = location
self.salary = salary
self.status = status
class Manager(Employee):
def __init__(self, ldap, name, location, salary, status, reportees):
self.name = name
self.reportees = reportees
self.location = location
print 'Manager has been created.'
def GetLocations(self):
for location in [Employee]:
print Employee.location
employee1 = Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active')
employee2 = Employee('slash', 'Slash', 'Dublin', 50000, 'active')
employee3 = Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', ['axlr', 'slash', 'peterp'])
答案 0 :(得分:2)
为什么不替换
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', ['axlr', 'slash', 'peterp'])
带
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', [employee1, employee2, employee3])
然后只是:
def GetLocations(self):
for emp in self.reportees:
print emp.location
答案 1 :(得分:1)
此:
for location in [Employee]:
print Employee.location
没有意义。您正在创建一个列表[Employee],其中不包含员工,而是Employee类本身。你想要像
这样的东西for employee in self.reportees:
print employee.location
但是你实际上没有将你的Manager实例传递给员工本身的任何连接,你只是给它一个名单列表。也许像是
def GetLocations(self):
for employee in self.reportees:
print employee.location
employees = [Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active'),
Employee('slash', 'Slash', 'Dublin', 50000, 'active'),
Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')]
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', employees)
>>> manager1.GetLocations()
Dublin
Dublin
New York
会给你你想要的东西。
答案 2 :(得分:1)
我会在Employee
类中添加一个静态的位置列表:
class Employee(object):
locations = []
def __init__(self, ldap, name, location, salary, status):
self.ldap = ldap
self.name = name
self.location = location
self.locations.append(location)
self.salary = salary
self.status = status
employee1 = Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active')
employee2 = Employee('slash', 'Slash', 'Dublin', 50000, 'active')
employee3 = Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')
print Employee.locations
答案 3 :(得分:0)
完整示例:
class Employee(object):
def __init__(self, ldap, name, location, salary, status):
self.ldap = ldap
self.name = name
self.location = location
self.salary = salary
self.status = status
class Manager(Employee):
def __init__(self, ldap, name, location, salary, status, reportees):
self.name = name
self.reportees = reportees
self.location = location
def addReportee(self, reportee):
self.reportees.append(reportee)
def GetLocations(self):
for reportee in self.reportees:
print reportee.location
employee1 = Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active')
employee2 = Employee('slash', 'Slash', 'Dublin', 50000, 'active')
employee3 = Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', [employee1, employee2, employee3])
# and if you wanted to add more:
#manager1.addReportee(employee4)
#manager1.addReportee(employee5)
#manager1.addReportee(employee6)
答案 4 :(得分:0)
class Employee(object):
def __init__(self, ldap, name, location, salary, status):
self.ldap = ldap
self.name = name
self.location = location
self.salary = salary
self.status = status
class Manager(Employee):
def __init__(self, ldap, name, location, salary, status, reportees):
self.name = name
self.reportees = reportees
self.location = location
print 'Manager has been created.'
# loop through that list of employees and print their locations
def GetLocations(self):
for employee in self.reportees:
print employee.location
employee1 = Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active')
employee2 = Employee('slash', 'Slash', 'Dublin', 50000, 'active')
employee3 = Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')
# pass the employees to the manger
manager1 = Manager('wayneg', 'Wayne Gretzky', 'Dublin', 50000, 'active', [employee1,employee2, employee3])