lucy6666 发表于 2020-6-2 10:33:19

小白求助 怎么用用class储存员工的名字和工资

问题是这样的
创建一个代表员工的class Employee
员工有姓名和工资,之后将雇员总数存储在class变量empCount中

这个该怎么做呀

heidern0612 发表于 2020-6-2 10:39:48

定义个字典,键和值对应员工姓名和工资,然后for循环键值+=1,统计员工总数。

Twilight6 发表于 2020-6-2 10:43:37

class Employee:

    empCount = 0

    def __init__(self,name,money):
      self.name = name
      self.money = money
      Employee.empCount += 1

    def get_data(self):
      return '员工:'+self.name+'工资:'+str(self.money)


a1 = Employee('李华',1000)
a2 = Employee('小刚',1520)
a3 = Employee('小明',5000)

print('雇佣员工总数为:',Employee.empCount)
print(a1.get_data())
print(a2.get_data())
print(a3.get_data())
页: [1]
查看完整版本: 小白求助 怎么用用class储存员工的名字和工资