为什么会出现属性错误?未找到WorkingHours 属性?
class Employee:
def numberofWorkingHours(self):
self.WorkingHours = 45
def printnumberofWorkingHours(self):
print(self.WorkingHours)
class Trainee:
def numberofWorkingHours(self):
self.WorkingHours = 60
emp = Employee()
emp.printnumberofWorkingHours()
最佳答案
class Employee:
def __init__(self):
self.WorkingHours = 45
def printnumberofWorkingHours(self):
print(self.WorkingHours)
class Trainee:
def numberofWorkingHours(self):
self.WorkingHours = 60
emp = Employee()
emp.printnumberofWorkingHours()
将 numberofWorkingHours 方法替换为 __init__
关于python - 属性错误: 'Employee' object has no attribute 'WorkingHours' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50835149/