有 2 个类:class A和class B. 在class A,我想调用一个方法class B。同时,在class B,我想调用一个方法class A。喜欢:
class A(object):
"""docstring for A"""
def __init__(self):
self.str_A = 'this is class A'
def printA(self):
print self.str_A
def callB(self):
B.printB()
class B(object):
"""docstring for B"""
def __init__(self):
self.str_B = 'This is class B'
def printB(self):
print self.str_B
def callA(self):
A.printA()
Run Code Online (Sandbox Code Playgroud)
嗯,我修改一下我的表达方式。
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()
Run Code Online (Sandbox Code Playgroud)
这是我的两节课?
class Stock是一类以接收股票数据?并且RtnTick在class Stock可以自动更新。class TradeSystem是一类做一个GUI,然后点击按钮时,该程序可以使股票exchange.GUI必须实时显示数据,所以当RtnTick更新的数据,我想打电话给self.e1_str.set()在class TradeSystem显示。当我按一下按钮,我会打电话给TradeCommit在class Stock做出deal.And我省略了很多其他codes.These两个类是large.And的...你有一个想法来解决这个问题?我是新在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.
Run Code Online (Sandbox Code Playgroud)
您正在寻找类方法(另一种语言中的静态方法)
class A(object):
"""docstring for A"""
def __init__(self):
self.str_A = 'this is class A'
def printA(self):
print self.str_A
@classmethod
def CB(cls):
B().printB()
class B(object):
"""docstring for B"""
def __init__(self):
self.str_B = 'This is class B'
def printB(self):
print self.str_B
@classmethod
def CA(cls):
A().printA()
aaa = A()
bbb = B()
aaa.CB() or bbb.CA()
>>> This is class B
>>> this is class A
Run Code Online (Sandbox Code Playgroud)
但是由于 printA() 和 printB() 需要一个实例,因此您需要实例化一个对象来调用它们
| 归档时间: |
|
| 查看次数: |
3521 次 |
| 最近记录: |