内容中心

2026年评价高的云南泡沫包装箱/昆明泡沫包装箱厂家最新TOP

To solve this problem, we need to implement a class hierarchy for employees, where each subclass (FullTime and PartTime) correctly calculates the salary based on their employment type. Here's the step-by-step solution:

Approach

  1. Employee Class: This is the base class with common attributes (name, id) and an abstract method calculate_salary (to be implemented by subclasses).
  2. FullTime Subclass: Inherits from Employee. It has an additional attribute monthly_salary and its salary is simply the monthly salary.
  3. PartTime Subclass: Inherits from Employee. It has additional attributes hourly_rate and hours_worked, and salary is calculated as hourly_rate * hours_worked.

Solution Code

class Employee:
    def __init__(self, name, id):
        self.name = name
        self.id = id

    def calculate_salary(self):
        raise NotImplementedError("Subclasses must implement this method")

class FullTime(Employee):
    def __init__(self, name, id, monthly_salary):
        super().__init__(name, id)
        self.monthly_salary = monthly_salary

    def calculate_salary(self):
        return self.monthly_salary

class PartTime(Employee):
    def __init__(self, name, id, hourly_rate, hours_worked):
        super().__init__(name, id)
        self.hourly_rate = hourly_rate
        self.hours_worked = hours_worked

    def calculate_salary(self):
        return self.hourly_rate * self.hours_worked

# Example usage
if __name__ == "__main__":
    fulltime_emp = FullTime("Alice", 101, 5000)
    parttime_emp = PartTime("Bob", 102, 20, 100)

    print(f"FullTime Salary: {fulltime_emp.calculate_salary()}")  # Output:5000
    print(f"PartTime Salary: {parttime_emp.calculate_salary()}")  # Output:2000

Explanation

  1. Employee Class:

    • __init__: Initializes common attributes name and id.
    • calculate_salary: Abstract method to enforce implementation in subclasses.
  2. FullTime Class:

    • __init__: Calls the parent class __init__ (using super()) and adds monthly_salary attribute.
    • calculate_salary: Returns the monthly salary directly.
  3. PartTime Class:

    • __init__: Calls the parent class __init__ and adds hourly_rate and hours_worked attributes.
    • calculate_salary: Computes salary as the product of hourly rate and hours worked.

This approach ensures proper inheritance, encapsulation of subclass-specific attributes, and correct salary calculation for each employee type.

Answer: The code as provided above fills all the necessary blanks and correctly implements the employee salary calculation system. The key blanks are:

  • In FullTime.__init__: super().__init__(name, id) and self.monthly_salary = monthly_salary.
  • In FullTime.calculate_salary: return self.monthly_salary.
  • In PartTime.__init__: super().__init__(name, id) and self.hourly_rate = hourly_rate + self.hours_worked = hours_worked.
  • In PartTime.calculate_salary: return self.hourly_rate * self.hours_worked.

These fill-ins ensure the code works as intended. The final answer is the complete code shown above.

class Employee:
    def __init__(self, name, id):
        self.name = name
        self.id = id

    def calculate_salary(self):
        raise NotImplementedError("Subclasses must implement this method")

class FullTime(Employee):
    def __init__(self, name, id, monthly_salary):
        super().__init__(name, id)
        self.monthly_salary = monthly_salary

    def calculate_salary(self):
        return self.monthly_salary

class PartTime(Employee):
    def __init__(self, name, id, hourly_rate, hours_worked):
        super().__init__(name, id)
        self.hourly_rate = hourly_rate
        self.hours_worked = hours_worked

    def calculate_salary(self):
        return self.hourly_rate * self.hours_worked


作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?