鱼C论坛

 找回密码
 立即注册
查看: 4082|回复: 15

请问一下类中的专有属性、专有变量

[复制链接]
发表于 2022-10-21 17:35:40 | 显示全部楼层
你的问题中Order这个类的total可以写成函数形式,也可以写成self.total的形式,就像前面的人说的那样。

而且看了你的代码我觉得有些问题,你虽然使用了ABC进行抽象,但是似乎自己都没有规划好每个接口应该具备怎样的能力。从设计模式的角度来说,你虽然使用了接口,但是却违背了里氏替换原则和依赖倒置原则。所以我把你的代码顺序进行了大幅改动,逻辑微调(就是Order类的total方法那个部分),保留了你要的total,也删除了__total这个私有属性,希望修改后的代码能对你有用。另外请注意,我使用了Python 3.10中的新特性来标注参数类型与返回值类型:
  1. from abc import ABCMeta, abstractmethod
  2. from collections import namedtuple

  3. # ----------抽象层,定义各种接口----------
  4. Customer = namedtuple('Customer', 'name fidelity')


  5. class InterfaceItem(metaclass=ABCMeta):
  6.     """商品接口,子类须具备product、price、quantity属性,
  7.     必须实现total方法
  8.     """

  9.     product: str
  10.     price: int | float
  11.     quantity: int | float

  12.     @abstractmethod
  13.     def total(self) -> int | float:
  14.         """商品总价"""
  15.         pass


  16. class InterfaceOrder(metaclass=ABCMeta):
  17.     """订单接口,子类须具备cart、customer、total属性,
  18.     且必须实现due方法
  19.     """

  20.     cart: list[InterfaceItem]
  21.     customer: Customer

  22.     @property
  23.     @abstractmethod
  24.     def total(self) -> int | float:
  25.         """订单商品的总金额"""
  26.         pass

  27.     @abstractmethod
  28.     def due(self) -> int | float:
  29.         """应该支付金额"""
  30.         pass


  31. class InterfacePromotion(metaclass=ABCMeta):
  32.     """促销活动接口,子类必须实现discount方法"""

  33.     @abstractmethod
  34.     def discount(self, order: InterfaceOrder) -> int | float:
  35.         """返回折扣金额(正值)"""
  36.         pass


  37. # ----------实现层,实现各种接口----------
  38. class LineItem(InterfaceItem):
  39.     """具体商品,实现IterfaceItem接口"""

  40.     def __init__(
  41.         self,
  42.         product: str,
  43.         quantity: int | float,
  44.         price: int | float
  45.     ) -> None:
  46.         self.product = product
  47.         self.quantity = quantity
  48.         self.price = price

  49.     def total(self) -> int | float:
  50.         return self.price * self.quantity


  51. class Order(InterfaceOrder):
  52.     """具体订单,实现IterfaceOrder接口"""

  53.     def __init__(
  54.             self,
  55.             customer: Customer,
  56.             *cart: InterfaceItem,
  57.             promotion: InterfacePromotion = None
  58.     ) -> None:
  59.         self.customer = customer
  60.         self.cart = list(cart)
  61.         self.promotion = promotion

  62.     @property
  63.     def total(self) -> int | float:
  64.         if self.cart:
  65.             return sum(item.total() for item in self.cart)
  66.         return 0

  67.     def due(self) -> int | float:
  68.         if self.promotion is None:
  69.             discount = 0
  70.         else:
  71.             discount = self.promotion.discount(self)
  72.         return self.total - discount

  73.     def __repr__(self):
  74.         fmt = '<Order total: {:.2f} due: {:.2f}>'
  75.         return fmt.format(self.total, self.due())


  76. class FidelityPromo(InterfacePromotion):
  77.     """为积分为1000或以上的顾客提供%5的折扣"""

  78.     def discount(self, order: InterfaceOrder) -> int | float:
  79.         return order.total * .05 if order.customer.fidelity >= 1000 else 0


  80. class BulkItemPromo(InterfacePromotion):
  81.     """单个商品为20个以上时提供10%折扣"""

  82.     def discount(self, order: InterfaceOrder) -> int | float:
  83.         discount = 0
  84.         for item in order.cart:
  85.             if item.quantity >= 20:
  86.                 discount += item.total() * .1
  87.         return discount


  88. class LargeOrderPromo(InterfacePromotion):
  89.     """订单中的不同商品达到10个以上时提供7%折扣"""

  90.     def discount(self, order: InterfaceOrder) -> int | float:
  91.         distinct_items = {item.product for item in order.cart}
  92.         if len(distinct_items) >= 10:
  93.             return order.total * .07
  94.         return 0


  95. # ----------业务层,处理具体业务逻辑----------
  96. def business() -> None:
  97.     # 实例化各种促销方案
  98.     fp: InterfacePromotion = FidelityPromo()

  99.     c1: Customer = Customer('张三', 1000)
  100.     goods: list[InterfaceItem] = [
  101.         LineItem('商品A', 20, 126), LineItem('商品B', 11, 51.8), LineItem('商品C', 3, 26)
  102.     ]
  103.     order: Order = Order(c1, *goods, promotion=fp)
  104.     print(order)


  105. if __name__ == '__main__':
  106.     business()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-5 04:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表