实现抽象类之方式一
# 抽象类: # 是一个开发的规范,约束它的所有子类必须实现一些和它同名的方法 class Pay(object): # 抽象类 def pay(self, money): """只要见到项目中有这种类,你的子类中必须实现和play同名的方法""" raise NotImplementedError('请在子类中重写与父类同名的方法') class AliPay(Pay): def __init__(self, name): self.name = name def pay(self, money): dic = {'uname':self.name, 'price':money} print(f'{self.name}通过支付宝成功支付{money}元!') class WeChatPay(Pay): def __init__(self, name): self.name = name def pay(self, money): dic = {'uname':self.name, 'price':money} print(f'{self.name}通过微信成功支付{money}元!') class ApplePay(Pay): def __init__(self, name): self.name = name def pay(self, money): dic = {'uname': self.name, 'price': money} print(f'{self.name}通过苹果支付支付了{money}元!') # aw = WeChatPay('alex') # aw.pay(400) # # aa = AliPay('wusir') # aa.pay(300) # 归一化设计: def pay(name, price, pay_kind): if pay_kind == 'WeChat': obj = WeChatPay(name) elif pay_kind == 'AliPay': obj = AliPay(name) elif pay_kind == 'ApplePay': obj = ApplePay(name) obj.pay(price) pay('alex', 400, 'WeChat') pay('wusir', 300, 'AliPay') pay('eva-j', 500, 'ApplePay')