python实现类之间的方法互相调用

时间:2019-04-14
本文章向大家介绍python实现类之间的方法互相调用,主要包括python实现类之间的方法互相调用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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类的方法。

以上这篇python实现类之间的方法互相调用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。