1
我有一个班,另一个子类。我想跳过初始化过程的一部分,例如:跳过超类__init__的一部分?
class Parent:
__init__(self, a, b, c, ...):
# part I want to keep:
self.a = a
self.b = b
self.c = c
...
# part I want to skip, which is memory and time consuming
# but unnecessary for the subclass:
self.Q = AnotherClass()
class Child(Parent):
__init__(self):
#a part of the parent initialization process, then other stuff
这两种方案我已经出来是:
- 充分利用
Parent
类的抽象类的父其中止跌“T包括初始化为Child
的不需要的部分,或 - 重复这一点我想在孩子家长的初始化部分
哪个最好,还是有更好的方法?
从逻辑上讲,它更符合面向对象的原则,创建一个只有你想在每个实例中使用的东西(如你所建议的)并继承它的基类。这可能会导致代码中出现不必要的复杂性,但可能更适合进行我个人感觉的奇怪黑客攻击。 –
父级是否受您控制,还是第三方级别? – snapshoe
@snapshoe它在我的控制下 –