加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 21:01:00 所属栏目: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-------------

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实现类之间的方法互相调用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • Python实现子类调用父类的方法
  • Python2.7 实现引入自己写的类方法
  • Python 实现引用其他.py文件中的类和类的方法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读