我想为我在Employee类中创建的每个员工提供一个ID, 但自动
代码如下:
class employees :
def __init__(self,first,last,pay):
self.first_name = first
self.last_name = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
self.full_name = first + ' '+last
self.facebook_link = 'FB.com/'+ self.full_name
答案 0 :(得分:1)
class employees:
uid = 0
def __init__(self,first,last,pay):
self.first_name = first
self.last_name = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
self.full_name = first + ' '+last
self.facebook_link = 'FB.com/'+ self.full_name
employees.uid += 1
self.uid = employees.uid
现在,当我按实例创建并打印其uid
emp1 = employees('Abhishek', 'Babuji', 1000)
print(emp1.uid)
输出:1
emp2 = employees('Abhishek1', 'Babuji1', 10001)
print(emp2.uid)
输出:2
每次进入__init__
employees.uid
都会增加1,然后使用self.uid
将其分配给实例