所以我让我的代码工作,但是有两行让我感到困惑,并且不太确定要放什么。
代码(两行标记):
class Person(object):
numPerson = 0
def __init__(self,firstName,lastName):
self.firstName = firstName
self.lastName = lastName
def fullName(self):
print self.firstName +' '+self.lastName
class Employee(Person):
numEmployee = 0
def __init__(self,firstName,lastName,pay,employID):
Person.__init__(self, firstName, lastName)
self.pay = pay
self.employID = employID
Employee.numEmployee += 1
class Programmer(Employee):
def __init__(self,firstName,lastName,pay,employID,proLang):
self.proLang = proLang
Employee.__init__(self, firstName, lastName, pay, employID)
class Manager(Employee):
def __init__(self,firstName,lastName,pay,employID,progList):
self.progList = progList
Employee.__init__(self, firstName, lastName, pay, employID)
def addProgrammer(self):
self.progList.append(Programmer.fullName) <------------------- This line
def removeProgrammer(self):
if len(self.progList) == 0:
pass
else:
del self.progList[0]
def printList(self):
print self.progList
a = Manager('Alfred','Jones',20.00,0001,[])
b = Programmer('James','Smith', 11.75, 0002, 'Java')
a.addProgrammer() <--------------------------------------------- And This line
a.printList()
我尝试使用progList
方法将程序员的名称添加到.addProgramer
。我一直在尝试不同的组合,这是我最接近的。
输出:
[<unbound method Programmer.fullName>]
所以,我不确定在addProgramer
方法中需要什么才能正确添加程序员名称,或者如果我需要在最后a.addProgrammer
内的参数
答案 0 :(得分:4)
下面:
self.progList.append(Programmer.fullName)
您没有添加程序员的实例,而是从程序员类中添加一个方法。
此外:
def fullName(self):
print self.firstName +' '+self.lastName
这实际上并不返回程序员的名字,它只会将其打印到控制台。要实际输出并使用return self.firstName + ' ' + self.lastName
同样在该函数中,您还需要指定要添加的程序员:
def addProgrammer(self, added_programmer):
self.progList.append(added_programmer.fullName()) # Call the function to get the fullname
现在要添加程序员:
Alfred = Manager('Alfred','Jones',20.00,0001,[]) #make a manager
James = Programmer('James','Smith', 11.75, 0002, 'Java') #make a programmer
Alfred.addProgrammer(James) #add the programmer
Alfred.printList()
把这一切放在一起:
class Person(object):
numPerson = 0
def __init__(self,firstName,lastName):
self.firstName = firstName
self.lastName = lastName
def fullName(self):
return self.firstName +' '+self.lastName
class Employee(Person):
numEmployee = 0
def __init__(self,firstName,lastName,pay,employID):
Person.__init__(self, firstName, lastName)
self.pay = pay
self.employID = employID
Employee.numEmployee += 1
class Programmer(Employee):
def __init__(self,firstName,lastName,pay,employID,proLang):
self.proLang = proLang
Employee.__init__(self, firstName, lastName, pay, employID)
class Manager(Employee):
def __init__(self,firstName,lastName,pay,employID,progList):
self.progList = progList
Employee.__init__(self, firstName, lastName, pay, employID)
def addProgrammer(self, added_programmer):
self.progList.append(added_programmer.fullName()) # Call the function to get the fullname
def removeProgrammer(self):
if len(self.progList) == 0:
pass
else:
del self.progList[0]
def printList(self):
print self.progList
Alfred = Manager('Alfred','Jones',20.00,1,[])
James = Programmer('James','Smith', 11.75, 2, 'Java')
Alfred.addProgrammer(James)
Alfred.printList()