首页 > 解决方案 > 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)

标签: pythonpython-3.x

解决方案


我会弄清楚你的台词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)

推荐阅读