避免破坏 python 子类中的 liskov 替换原则

问题描述

假设有一个执行复杂操作以产生结果的类。然后我想要一堆子类,它们做同样的事情,但将结果变异为另一个实例。这适用于 python 并创建了一种生成每个子类的相对不错的方法调用 super 并继续执行操作),但我知道这违反了 Liskov 替换原则。我将如何重构它以遵守该原则?

class Super:
   def produce(value: int) -> int:
       return value * 2 # imagine this is a complex operation. 

class Sub(Super):
   def produce(value: int) -> str:
       return str(super().produce(value)). # converting to another type

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)