Class BigClassA:
def __init__(self):
self.a = 3
def foo(self):
self.b = self.foo1()
self.c = self.foo2()
self.d = self.foo3()
def foo1(self):
# do some work using other methods not listed here
def foo2(self):
# do some work using other methods not listed here
def foo3(self):
# do some work using other methods not listed here
Class BigClassB:
def __init__(self):
self.b = # need value of b from BigClassA
self.c = # need value of c from BigClassA
self.d = # need value of d from BigClassA
def foo(self):
self.f = self.bar()
def bar(self):
# do some work using other methods not listed here and the value of self.b, self.c, and self.d
Class BigClassC:
def __init__(self):
self.b = # need value of b from BigClassA
self.f = # need value of f from BigClassB
def foo(self):
self.g = self.baz()
def baz(self):
# do some work using other methods not listed here and the value of self.b and self.g
问题: 基本上我有3个类有很多的方法,它们有点依赖,你可以从代码中看到。如何将BigClassA中的实例变量self.b,self.c,self.d的值分享给BigClassB?Python:如何在不同类的实例之间共享数据?
nb:这3个类不能彼此继承,因为它没有意义。
我想到的只是将所有方法合并为一个超级大类。但我不认为这是一个正确的方式来做到这一点。
'类BigClassB(BigClassA):'和'类BigClassC(BigClassA,BigClassB)'。使用继承 – ForceBru 2015-03-03 11:50:00
您可以尝试使用构图。那就是当你在B类的一个实例中实例化一个类A的对象时。这样你就可以拥有连接,而不用说其中的一个是专门化的。 [阅读](http://learnpythonthehardway.org/book/ex44.html)。除此之外,您可以发送类A的对象并将您需要的属性分配给类B中的新属性。尽管这可能会破坏数据一致性。 – ljetibo 2015-03-03 11:50:07
你想分享每个班级的实例还是全球的数据? – 2015-03-03 11:50:36