python类之间方法互相调用

来源:互联网 发布:小区物业管理系统源码 编辑:程序博客网 时间:2024/06/09 12:01

all.py

from son import *
class ALL():
    def __init__(self):
        self.mSon = SON(self)

    def getAll(self):
        print "=================getall---------------"
        return self.mSon.getSon()

    def getAlltest(self):
        print "=================getAlltest---------------"

Instance = ALL()

Instance.getAll()


son.py

class SON():
    def __init__(self, instance):
    self.mBattle = instance

    def getSon(self):
    return self.mBattle.getAlltest()


son.py和all.py在同一个文件夹下面,可以通过from son import *或者 import son 来互相调用。

可以动态实例化son.py里面的SON类,而且也可以把ALL的实例通过参数传递给SON,从而调用ALL类的方法。