[英]Create child class object in parent class
is it a good design to create object of child class in parent like the example below, it seems to be working but is it a good design, is there a better way to do that?像下面的例子一样在父类中创建子类的对象是一个很好的设计,它似乎工作但它是一个很好的设计,有没有更好的方法来做到这一点?
class parent(object):
def __init__(self):
print('Im running')
def execute(self):
x = child()
x.run()
x.myfun()
def myfun(self):
print('parent function')
def run(self):
print('parent running')
class child(parent):
def __init__(self):
super().__init__()
print('Im running too')
def run(self):
print('child running')
f = parent()
f.execute()
This is definitely not a good design for your problem, and not a good design generally (bar exceptions which I cannot think of any), and is definitely against OOP design and SOLID principles.对于您的问题,这绝对不是一个好的设计,通常也不是一个好的设计(我想不出任何例外),并且绝对违反 OOP 设计和 SOLID 原则。
Simply in OOP design, or any other software engineering frame of mind, you want clear relations.只是在 OOP 设计或任何其他软件工程思维框架中,您需要明确的关系。 This makes the relationship between your parent class and your child class inherently more complex.
这使得您的父类和子类之间的关系本质上更加复杂。 Not to mention most of the other languages (at least languages which run complied code) would not allow such thing to happen.
更不用说大多数其他语言(至少是运行编译代码的语言)不允许这样的事情发生。
If you need to have an instance of one in the other and vice versa, maybe inheritance was the wrong pattern to begin with, since your classes seem to be connected in a two-way manner unlike scenarios in which inheritance is employed.如果您需要在另一个中拥有一个实例,反之亦然,那么继承可能是错误的模式,因为您的类似乎以双向方式连接,这与使用继承的场景不同。
The fact that execute
doesn't use self
at all suggests it should be a class method, in which case you can use whichever class is actually provided to instantiate x
. execute
根本不使用self
的事实表明它应该是一个类方法,在这种情况下,您可以使用实际提供的任何类来实例化x
。
Once you've done this, the definition of Parent
no longer relies on any particular subclass;一旦你这样做了,
Parent
的定义就不再依赖于任何特定的子类; in fact, it doesn't rely on the fact that Parent
is subclassed at all ;事实上,它不依赖于一个事实,即
Parent
在所有子类; Parent.execute()
will continue to work. Parent.execute()
将继续工作。
For example,例如,
class Parent:
def __init__(self):
print('Im running')
@classmethod
def execute(cls):
x =
cls()
x.run()
x.myfun()
def myfun(self):
print('parent function')
def run(self):
print('parent running')
class Child(Parent):
def __init__(self):
super().__init__()
print('Im running too')
def run(self):
print('child running')
Child.execute()
This will output这将输出
Im running
Im running too
child running
parent function
Since Child.execute
isn't defined, it resolves to Parent.execute
.由于
Child.execute
没有定义,它解析为Parent.execute
。 But Child
is still the first argument passed.但是
Child
仍然是第一个通过的参数。 As a result, x
will be an instance of Child
, not Parent
.因此,
x
将是Child
的实例,而不是Parent
。 x.run()
thus runs Child.run
, but x.myfun()
runs Parent.myfun
. x.run()
因此运行Child.run
,但x.myfun()
运行Parent.myfun
。
The fact that Parent.execute
, though, still depends on x
having an attribute specific to cls
suggests that you should defer restrict execute
to using only things defined by Parent
, and let a child override execute
to add any child-specific behavior.但是,
Parent.execute
仍然依赖于x
具有特定于cls
的属性这一事实表明您应该推迟限制execute
以仅使用Parent
定义的内容,并让子覆盖execute
以添加任何特定于子的行为。
Or, execute
should be an instance method, but it should simply call self.fun
, putting the burden on the caller to call execute
with an appropriate object.或者,
execute
应该是一个实例方法,但它应该简单地调用self.fun
,将负担放在调用者身上,以使用适当的对象调用execute
。
c = Child()
c.execute()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.