【发布时间】:2020-06-15 19:27:37
【问题描述】:
class Employee:
def __init__(self, first,last, pay):
self.first=first
self.last= last
self.email=first+'.'+last+'@company.com'
def fullname(self):
return '{} {}'.format(self.first,self.last)
def apply_rate(self):
self.pay=int(self.pay * 1.04)
emp_1=Employee('Rehim','Hesimov',50000)
emp_2=Employee('Test','User',60000)
print(emp_1.pay)
emp_1.apply_rate()
print(emp_1.pay)
Traceback(最近一次调用最后一次):
文件“C:\Users\rehim\Desktop\class.py”,第 12 行,在 ''
打印(emp_1.pay)
AttributeError: 'Employee' 对象没有属性 'pay'
谁能帮我解决这个问题?
【问题讨论】:
-
__init__()函数不会分配self.pay。大概你打算这样做。
标签: python class methods attributes