【发布时间】:2020-06-17 19:38:56
【问题描述】:
Initialize subclass within class in python 中提出了类似的问题。那里的答案得出的结论是应该避免这种方法,我不确定这是否适用于以下情况,我想知道如何实现它,或者我应该怎么做。
class Rational():
def __init__(self, numerator, denominator):
self.numerator = numerator
self.denominator = denominator
if self.denominator == 1:
pass
# >>> How to initialize as Integer in this case? <<<
# ... More methods for rationals
class Integer(Rational):
def __init__(self, value):
super().__init__(value, 1)
self.value = value
# ... More methods for integers
在这个文件中,我有简单的整数类和Rational 数字类。由于所有Integers 都是有理数,Integer 是有理数的子类,我们调用super() 以允许整数调用有理数的方法。
不过,如果 Rational 用分母 1 初始化时,它可以被自动识别为整数,那就太好了。例如,我希望 x = Rational(4, 1) 然后允许我调用 x.integer_method() 甚至让 Integer(4) == x 返回 True。问题是,我不知道我是否可以从 Rational 初始化程序中调用 Integer 初始化程序,因为我可能会陷入无限循环。
解决这个问题的最佳方法是什么(通常不仅适用于整数和有理数,而且适用于任何父子类型,其中 Parent 的实例在语义上可能不被识别为 Child 类型的成员直到初始化时间?
【问题讨论】:
-
听起来像教科书用例,用于替换构造函数的工厂方法。
-
如果为
Rational类定义了一个__new__()方法,当条件满足时,它可以返回一个Integer的实例。例如,请参阅我对问题 Improper use of _new_ to generate classes? 的回答。 -
糟糕的设计模式,不是很稳固
-
@Pynchia:不是最好的评论,因为它假设每个人都知道SOLID 的含义。
标签: python class inheritance