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
- Employee Class: This is the base class with common attributes (
name,id) and an abstract methodcalculate_salary(to be implemented by subclasses). - FullTime Subclass: Inherits from Employee. It has an additional attribute
monthly_salaryand its salary is simply the monthly salary. - PartTime Subclass: Inherits from Employee. It has additional attributes
hourly_rateandhours_worked, and salary is calculated ashourly_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
-
Employee Class:
__init__: Initializes common attributesnameandid.calculate_salary: Abstract method to enforce implementation in subclasses.
-
FullTime Class:
__init__: Calls the parent class__init__(usingsuper()) and addsmonthly_salaryattribute.calculate_salary: Returns the monthly salary directly.
-
PartTime Class:
__init__: Calls the parent class__init__and addshourly_rateandhours_workedattributes.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)andself.monthly_salary = monthly_salary. - In
FullTime.calculate_salary:return self.monthly_salary. - In
PartTime.__init__:super().__init__(name, id)andself.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
作者声明:本文包含人工智能生成内容。