1.编写一个Date类,有checkMonth和checkDay函数以检查月份和日期的合法性,有判断是否为闰年的函数,设计定义set类函数,用以设定年,月份和日期,设计定义多个get函数用以获取年、月和日。 2. 创建一个抽象类employee类,该类中有姓名(sting 类型),ID(sting 类型)和出生日期(Date类型)。定义构造函数初始化每个成员;定义changeName()函数用来修改姓名,定义Display()函数用来显示每个员工的基本信息。纯虚函数为发放工资函数。 3.创建由Employee类派生出的其它四个员工类Salaried Employee,Commission Employee,Hourly Employee,以及由Commission Employee派生出的BasePlus Commission Employee。编写他们的新增成员、构造函数等,他们都有一个计算工资的函数。
时间: 2024-03-26 16:38:21 浏览: 114
php使用strtotime和date函数判断日期是否有效代码分享
1. Date类的代码实现如下:
```python
class Date:
def __init__(self):
self.year = None
self.month = None
self.day = None
def set_date(self, year, month, day):
if self.check_month(month) and self.check_day(year, month, day):
self.year = year
self.month = month
self.day = day
else:
print("Invalid date!")
def check_month(self, month):
return month in range(1, 13)
def check_day(self, year, month, day):
if month == 2:
if self.is_leap_year(year):
return day in range(1, 30)
else:
return day in range(1, 29)
elif month in [4, 6, 9, 11]:
return day in range(1, 31)
else:
return day in range(1, 32)
def is_leap_year(self, year):
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
def get_year(self):
return self.year
def get_month(self):
return self.month
def get_day(self):
return self.day
```
2. 抽象类Employee类的代码实现如下:
```python
from abc import ABCMeta, abstractmethod
class Employee(metaclass=ABCMeta):
def __init__(self, name, id, birth_date):
self.name = name
self.id = id
self.birth_date = birth_date
def change_name(self, new_name):
self.name = new_name
def display(self):
print(f"Name: {self.name}")
print(f"ID: {self.id}")
print(f"Birth date: {self.birth_date.get_year()}/{self.birth_date.get_month()}/{self.birth_date.get_day()}\n")
@abstractmethod
def pay_salary(self):
pass
```
3. 四个员工类和计算工资函数的代码实现如下:
```python
class SalariedEmployee(Employee):
def __init__(self, name, id, birth_date, monthly_salary):
super().__init__(name, id, birth_date)
self.monthly_salary = monthly_salary
def pay_salary(self):
return self.monthly_salary
class CommissionEmployee(Employee):
def __init__(self, name, id, birth_date, sales, commission_rate):
super().__init__(name, id, birth_date)
self.sales = sales
self.commission_rate = commission_rate
def pay_salary(self):
return self.sales * self.commission_rate
class HourlyEmployee(Employee):
def __init__(self, name, id, birth_date, hourly_wage, hours_worked):
super().__init__(name, id, birth_date)
self.hourly_wage = hourly_wage
self.hours_worked = hours_worked
def pay_salary(self):
if self.hours_worked <= 40:
return self.hourly_wage * self.hours_worked
else:
return self.hourly_wage * 40 + self.hourly_wage * 1.5 * (self.hours_worked - 40)
class BasePlusCommissionEmployee(CommissionEmployee):
def __init__(self, name, id, birth_date, sales, commission_rate, base_salary):
super().__init__(name, id, birth_date, sales, commission_rate)
self.base_salary = base_salary
def pay_salary(self):
return super().pay_salary() + self.base_salary
```
这些类可以用来创建具体的员工对象,并调用其方法来进行操作,比如:
```python
# 创建一个Date对象
d = Date()
# 设置日期
d.set_date(2021, 2, 29)
# 创建一个SalariedEmployee对象
se = SalariedEmployee("Tom", "001", d, 5000)
# 显示员工信息
se.display()
# 计算工资
salary = se.pay_salary()
print(f"Salary: {salary}")
```
阅读全文