【发布时间】:2020-12-29 22:14:24
【问题描述】:
import datetime
class Employee:
def __init__ (self,first,last,pay,dob):
self.first = first
self.last = last
self.pay = pay
self.email = first + "." + last + "@company.com"
self.dob = dob
def fullname(self):
return '{} {}'.format(self.first,self.last)
def age(dob):
today = date.today()
return today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))
emp1 = Employee("aa","ss",122,date(1991,2,3))
emp2 = Employee("ww","ii",637,date(1997,8,24))
emp3 = Employee("ee","oo",986,date(1986,10,19))
#driver code
print(Employee.age(emp2))
我收到以下错误:
AttributeError: 'Employee' 对象没有属性 'year'
这有什么不正确的?
【问题讨论】:
-
方法的第一个参数是
self,而不是dob。你在def age(dob)中调用的dob实际上是Employee实例。
标签: python oop python-class