python如何在两个类之间相互调用?
有两类:python如何在两个类之间相互调用?,python,Python,有两类:A类和B类。在类A中,我想调用类B中的方法。同时,在类B中,我想调用类a中的一个方法。 比如: 我怎样才能做到这一点? 嗯,我修正了我的表情 class A(object): """docstring for A""" def __init__(self): self.str_A = 'this is class A' def printA(self): print self.str_A There is a functio
A类
和B类
。在类A
中,我想调用类B
中的方法。同时,在类B
中,我想调用类a
中的一个方法。
比如:
我怎样才能做到这一点?
嗯,我修正了我的表情
class A(object):
"""docstring for A"""
def __init__(self):
self.str_A = 'this is class A'
def printA(self):
print self.str_A
There is a function to call printB in class B names CB.
class B(object):
"""docstring for B"""
def __init__(self):
self.str_B = 'This is class B'
def printB(self):
print self.str_B
There is a function to call printA in class A names CA.
aaa = A()
bbb = B()
aaa.CB() or bbb.CA()
当然,上面的句子是错误的。我怎样才能做到这一点?python如何在两个类之间相互调用?
以下是我的两门课:
class Stock
是一个接收股票数据的类,class Stock
中的RtnTick
可以自动更新。class TradeSystem
是一个制作GUI的类,当点击按钮时,程序可以制作股票交易所。GUI必须实时显示数据,因此当RtnTick
更新数据时,我想在class TradeSystem
中调用self.e1_str.set()
进行显示。当我点击按钮时,我会在class Stock
中调用TradeCommit
进行交易。我省略了很多其他代码。这两个类都很大。而且……你有办法解决这个问题吗?我是python新手。谢谢
class Stock(LtsAPI):
def RtnTick(self,t):
global Sto,Configs_Path,Sto,AskPri,BidPri,AskVol,BidVol
contract = t.InstrumentID
if(contract in Sto):
#Here I want to call **self.e1_str.set()** in class TradeSystem.
def TradeCommit(self):
#This is a function to trading.
class TradeSystem(object):
def __init__(self):
self.root = Tk()
self.e1_str = StringVar()
self.e1 = Entry(self.root,textvariable = self.e1_str)
self.e1.bind('<KeyPress>')
self.e1.grid(row = 1, column = 0)
#**self.e1_str.set()** can set the content to display.
def Trade(self):
self.button = Button(self.root,command = *call **TradeCommit** in class Stock*)
#command is the function to run when triggered.
类别库存(LtsAPI):
def RtnTick(自身,t):
全局Sto、配置S_路径、Sto、AskPri、BidPri、AskVol、BidVol
合同=t.InstrumentID
如果(Sto中的合同):
#这里我想在TradeSystem类中调用**self.e1_str.set()**。
def TradeCommit(self):
#这是交易的一个功能。
类系统(对象):
定义初始化(自):
self.root=Tk()
self.e1_str=StringVar()
self.e1=Entry(self.root,textvariable=self.e1_str)
self.e1.bind(“”)
self.e1.grid(行=1,列=0)
#**self.e1_str.set()**可以设置要显示的内容。
def贸易(自助):
self.button=button(self.root,command=*调用**TradeCommit**in class Stock*)
#命令是触发时要运行的函数。
您正在寻找类metods(其他语言中的静态方法)
但是,由于printA()和printB()需要一个实例,您需要实例化一个对象来调用它们就在上周,我正在做一些类似的事情,我需要实例化类B中的类A和类A中的类B(循环依赖性问题)。这可以用于任何目的,在两个类对象之间共享数据,调用其他类对象的方法等。下面是我如何完成它的,如果这是您需要的,您可以根据需要编辑它
class A(object):
def __init__(self, b=None):
self.B = b or B(self)
self._number = None
def set_number(self, number):
self._number = number
def get_number(self):
return self._number
def a(self):
print 'Method "a" of "A"'
def b(self):
print 'Method "b" of "A"'
def c(self):
print 'Method "c" of "A"'
class B(object):
def __init__(self, a=None):
self.A = a or A(self)
self._number = None
def set_number(self, number):
self._number = number
def get_number(self):
return self._number
def a(self):
print 'Method "a" of "A"'
def b(self):
print 'Method "b" of "A"'
def c(self):
print 'Method "c" of "A"'
if __name__ == '__main__':
a = A()
b = B()
a.set_number(1)
print a.get_number()
a.a()
a.b()
a.c()
a.B.set_number(2)
print a.B.get_number()
a.B.a()
a.B.b()
a.B.c()
b.set_number(3)
print b.get_number()
b.a()
b.b()
b.c()
b.A.set_number(4)
print b.A.get_number()
b.A.a()
b.A.b()
b.A.c()
我想办法解决这个问题
class A(object):
"""docstring for A"""
def __init__(self):
pass
def FromAnother(self,s):
self.Object = s
def ExecAFunction(self,t):
self.Object.AFunctionFromA(t)
class B(object):
"""docstring for B"""
def __init__(self):
self.object = A()
self.object.FromAnother(self)
self.object.ExecAFunction('Call a function in B through A.')
def AFunctionFromA(self,t):
print t
b = B()
我在类B中实例化了类A。init。我使用刚才定义的实例将B本身传递给A的函数。接下来,我从A通过A中的函数ExecuteAFunction调用B的函数。因此,现在我可以在两个类之间调用函数。你觉得这个方法怎么样?在实例化对象的常规方法中,例如
A()。printA()
。printA/B是实例方法,应该在对象上调用,而不是通过没有任何对象作为参数的类来调用。class A和B只是解释我的问题的演示。我想知道这些问题的常见方法。更好的是,你应该完全删除这些类;他们没有在这里添加任何内容。使用独立函数。非常感谢。如果这两个类有很多方法,我只想使用其中一个,在另一个类中实例化一个类是否会影响程序的性能?您是否有更好的方法避免在两个类之间调用对方?如何将您的逻辑封装在函数中?谢谢。我重申了我的问题。您能给我一个建议吗?您应该实现观察者模式:谢谢。我重申了我的问题问题。你能给我一个建议吗?
class A(object):
def __init__(self, b=None):
self.B = b or B(self)
self._number = None
def set_number(self, number):
self._number = number
def get_number(self):
return self._number
def a(self):
print 'Method "a" of "A"'
def b(self):
print 'Method "b" of "A"'
def c(self):
print 'Method "c" of "A"'
class B(object):
def __init__(self, a=None):
self.A = a or A(self)
self._number = None
def set_number(self, number):
self._number = number
def get_number(self):
return self._number
def a(self):
print 'Method "a" of "A"'
def b(self):
print 'Method "b" of "A"'
def c(self):
print 'Method "c" of "A"'
if __name__ == '__main__':
a = A()
b = B()
a.set_number(1)
print a.get_number()
a.a()
a.b()
a.c()
a.B.set_number(2)
print a.B.get_number()
a.B.a()
a.B.b()
a.B.c()
b.set_number(3)
print b.get_number()
b.a()
b.b()
b.c()
b.A.set_number(4)
print b.A.get_number()
b.A.a()
b.A.b()
b.A.c()
class A(object):
"""docstring for A"""
def __init__(self):
pass
def FromAnother(self,s):
self.Object = s
def ExecAFunction(self,t):
self.Object.AFunctionFromA(t)
class B(object):
"""docstring for B"""
def __init__(self):
self.object = A()
self.object.FromAnother(self)
self.object.ExecAFunction('Call a function in B through A.')
def AFunctionFromA(self,t):
print t
b = B()