依赖关系
人玩游戏,依赖于电脑
class Person: def play(self,tools): #通过参数的传递把另外一个类的对象传过来 tools.run() #让工具运行 print("好开心,我可以玩游戏了!") class Ddiannao: def run(self): print("电脑开机可以运行->>>>>>>>>>") class Phone: def run(self): print("手机开机,可以正常运行->>>>>>>") d = Ddiannao() p = Person()
c = Person()c.play(d)
植物大战僵尸
class Plant: def __init__(self, name, hp, ad): # 200 self.name = name self.hp = hp self.ad = ad def gongji(self,js): print("植物攻击了僵尸") js.hp -= self.ad print("僵尸掉了%s滴血,还剩下%s滴血" % (self.ad,js.hp)) class Jiangshi: def __init__(self, name, hp, ad): # 200 self.name = name self.hp = hp self.ad = ad def gongji(self,zw): print("僵尸攻击了植物") zw.hp -= self.ad print("植物掉了%s滴血,还剩下%s滴血" % (self.ad,zw.hp)) p = Plant("豌豆射手",200,10) j = Jiangshi("铁通僵尸",200,10) p.gongji(j) p.gongji(j) p.gongji(j) j.gongji(p) j.gongji(p) j.gongji(p) j.gongji(p) j.gongji(p) j.gongji(p)
未完待续----------------------------------------------------
来源:https://www.cnblogs.com/yanpeizhang/p/10380930.html