python如何在两个类之间相互调用?

时间:2016-01-04 07:46:07

标签: python

有两个类:class Aclass 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()

我怎样才能做到这一点?

好吧,我修改了我的表情。

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是一个接收股票数据的类,RtnTick中的class Stock可以自动更新。class TradeSystem是一个用于制作GUI的类,当点击按钮时,程序可以库存交换.GUI必须实时显示数据,因此当RtnTick更新数据时,我想在self.e1_str.set()中调用class TradeSystem进行显示。当我点击按钮时,我会在TradeCommit中拨打class Stock进行交易。我省略了很多其他代码。这两个类很大。而且......你呢?有想法解决这个问题吗?我是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.

3 个答案:

答案 0 :(得分:4)

您正在寻找类metods(其他语言的静态方法)

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

但是由于printA()和printB()需要一个实例,你需要实例化一个对象来调用它们

答案 1 :(得分:1)

就在上周,我正在开发类似的东西,我需要在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()

答案 2 :(得分:0)

我找到了解决这个问题的方法。

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类中实例化了一个类。 init 。我使用我刚刚定义的实例将B本身传递给A函数。接下来我称之为B&#39的函数在A.So中,AFunctionFromA到函数Exec​​AFunction现在我可以调用两个类之间的函数。您如何看待这种方法?