python - Python 项目帮助(类/预期类型)
问题描述
我正在为学校做一个项目,模拟一个工资单程序,但我遇到了一个错误。我得到的错误是'预期类型'分类',而不是'员工''。相关代码是(我在产生错误的代码周围放了***,它是Employee类下的第5个函数)。
class Employee:
def __init__(self, emp_id, first_name, last_name, address, city, state, zipcode, clas = None):
self.emp_id = emp_id
self.first_name = first_name
self.last_name = last_name
self.address = address
self.city = city
self.state = state
self.zipcode = zipcode
self.classification = clas
def make_hourly(self, hourly_rate):
self.clas = Hourly(hourly_rate)
self.classification = self.clas
def make_salaried(self, salary):
self.clas = Salaried(salary)
self.classification = self.clas
def make_commissioned(self, salary, rate):
self.clas = Commissioned(rate, salary)
self.classification = self.clas
def issue_payment(self):
***pay = Classification.compute_pay(self)***
print('Mailing', pay, 'to', self.first_name, self.last_name, 'at', self.address, self.city, self.state, self.zipcode)
class Classification(ABC):
''' Interface for employee classifications '''
@abstractmethod
def compute_pay(self):
pass
class Hourly(Classification):
''' Manages timecard info. Computes pay '''
def __init__(self, hourly_rate):
self.hourly_rate = hourly_rate
self.timecards = [] # A list of floats representing hours worked
def compute_pay(self):
for i in list_of_timecards:
if i[0] == self.emp_id:
self.timecards.extend(i[1:])
total = list(map(float, self.timecards))
total = sum(total)
self.timecards.clear()
return total * self.hourly_rate
def add_timecard(self, hours):
self.timecards.append(hours)
class Salaried(Classification):
def __init__(self, salary):
self.salary = salary
def compute_pay(self):
return self.salary / 24
class Commissioned(Salaried):
def __init__(self, salary, commission_rate):
self.commission_rate = commission_rate
self.salary = salary
self.receipts = []
def add_receipt(self, amount):
self.receipts.append(amount)
def compute_pay(self):
for i in list_of_receipts:
if i[0] == self.emp_id:
self.receipts.extend(i[1:])
total = list(map(float, self.receipts))
total = sum(total)
self.receipts.clear()
return (self.salary / 24) + ((self.commission_rate / 100) * total)
我对这个问题的理解是,我需要将我的“员工”对象传递给“compute_pay”函数,然后将其传递给相关的子类(每小时等)以运行并返回结果。我尝试将 pay = Classification.compute_pay(self) 更改为 pay = Classification.compute_pay(self.clas) 但是返回错误 'AttributeError: 'Employee' object has no attribute 'clas' 这是没有意义的。也许是我没有正确地将员工分配到班级?代码是(它从 CSV 文件中提取,并且它正确提取数据并生成类对象,我已经检查过)
def load_employees():
f = open("employees.csv")
f.readline() # skip header line
for line in f:
fields = line.strip().split(',')
emp = Employee(*fields[:7])
if fields[7] == '3':
clas = Hourly(fields[10]) # Need to define Hourly
emp.classification = clas
elif fields[7] == '2':
clas = Commissioned(fields[8], fields[9])
emp.classification = clas
elif fields[7] == '1':
clas = Salaried(fields[8])
emp.classification = clas
employees.append(emp)
解决方案
我会弄清楚你的台词Classification.compute_pay(self)
:
- 分类 => 类分类
- 计算支付 => 类
- 方法 self => this = 一个 Employee 实例
pass
表示什么都不做,用于避免不必要的代码。
每个类方法都有self
一个参数来允许引用这个类的实例。要传递参数(这里是您的员工),请使用参数。同样实现父类的方法会覆盖此方法。每个函数compute_pay
都应该有第二个参数
def compute_pay(self, employee):
# do your stuff
然后你可以使用这条线issue_payment
pay = self.clas.compute_pay(self)
推荐阅读
- python-3.x - 将 tkinter 变量保存到 txt 文件错误
- go - 使用未从包中导出的类型
- c++ - 释放 PyBind11 中的 GIL 以在 Python 中使用 OpenMP 进行多线程
- localization - Web 应用程序全球化/本地化工具的选项?
- java - 我可以使用通过 ajax 传递的值来使用准备好的语句进行数据库搜索吗?
- python - 尝试解析电子邮件的 html 时,“不能在像对象这样的字节上使用字符串模式”
- recaptcha - 在没有 php 的情况下在 Github 页面上实现 recaptcha?
- docker-compose - docker-compose down 不会..“关闭”容器
- haskell - 为以 char 开头的字符串创建一个布尔值以返回 true,而后续值可以是 char 数字或下划线
- android - Kotlin WebView - 防止在新的 Chrome 浏览器窗口中打开