策略模式-Python

import sys
#现金收费抽象类
class CashSuper(object):
    def accept_cash(self,money):
        pass
#正常收费子类
class CashNormal(CashSuper):
    def accept_cash(self,money):
        return money

#打折收费子类
class CashRebate(CashSuper):
    def __init__(self,discount=1):
        self.discount = discount

    def accept_cash(self,money):
        return money * (self.discount)

#返利收费子类
class CashReturn(CashSuper):
    def __init__(self,money_condition=0,money_return=0):
        self.money_condition = money_condition
        self.money_return = money_return

    def accept_cash(self,money):
        if money>=self.money_condition:
            return money - (money / self.money_condition) * self.money_return
        return money

#具体策略类
class Context(object):
    def __init__(self,i):
        if i==1:
            self.csuper = CashNormal()
        elif i==2:
            self.csuper = CashRebate(0.8)
        elif i==3:
            self.csuper = CashReturn(0.2,10)
        else:
            self.csuper = CashNormal()

    def GetResult(self,money):
        return self.csuper.accept_cash(money)




if __name__ == '__main__':
    money = float(input("原价: "))
    mode = input("选择折扣方式: 1) 原价 2) 8折 3) 满100减10: ")
    mm = int(mode)
    m = Context(mm)
    t = m.GetResult(money)
    print("需要支付: ",t)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值