创建子类EmptyNestDiscount和SingleParentDiscount继承Discount类并实现getDiscount方法,依据优惠证类型和单人享受的减少折扣返回此优惠证的最终享受折扣(最终享受折扣=优惠证折扣基础 - 单人享受的减少折扣 * 享受优惠人数)(例如:空巢老人优惠证的单人享受减少折扣是1.2,家庭人数是2,那最终折扣为8-1.2*2=5.6折)。
时间: 2024-02-03 17:11:41 浏览: 19
下面是实现EmptyNestDiscount和SingleParentDiscount的代码:
```python
class Discount:
def __init__(self, discount_rate):
self.discount_rate = discount_rate
def get_discount(self, num_people):
return self.discount_rate * num_people
class EmptyNestDiscount(Discount):
def __init__(self, discount_rate, reduce_rate, num_people):
super().__init__(discount_rate)
self.reduce_rate = reduce_rate
self.num_people = num_people
def get_discount(self):
return self.discount_rate - self.reduce_rate * self.num_people
class SingleParentDiscount(Discount):
def __init__(self, discount_rate, reduce_rate, num_people):
super().__init__(discount_rate)
self.reduce_rate = reduce_rate
self.num_people = num_people
def get_discount(self):
return self.discount_rate - self.reduce_rate * self.num_people
```
在这里,我们定义了Discount类,它有一个构造函数和一个get_discount方法。然后我们创建了EmptyNestDiscount和SingleParentDiscount,它们都继承了Discount类并覆盖了get_discount方法。这两个子类都有一个构造函数,它们分别接受折扣率、单人享受减少折扣率和享受优惠人数。在get_discount方法中,我们计算出最终享受折扣并返回它。