【发布时间】:2016-04-10 05:43:29
【问题描述】:
创建两个类一个继承另一个类和将第一个类实例传递给另一个类的init方法有什么区别?
如下所示,
方法一:
class ht(object):
def __init__(self):
pass
def ch(self):
self.filename="hellgmhjkl"
class tt():
def __init__(self,ob1,ob3):
self.ob1=ob1
self.ob3=ob3
self.b=5
def display(self):
print(ob1.filename)
print(self.ob3.d)
class kk():
def __init__(self):
self.c=3
def hel(self):
self.d=self.c+5
if __name__ == '__main__':
ob1=ht()
ob1.ch()
ob3=kk()
ob3.hel()
ob2=tt(ob1,ob3)
ob2.display()
方法二:
class ht(object):
def __init__(self):
pass
def ch(self):
self.filename="hellgmhjkl"
class tt(ht):
def __init__(self,ob1,ob3):
self.ob1=ob1
self.ob3=ob3
self.b=5
def display(self):
print(ob1.filename)
print(self.ob3.d)
class kk():
def __init__(self):
self.c=3
def hel(self):
self.d=self.c+5
if __name__ == '__main__':
ob1=ht()
ob1.ch()
ob3=kk()
ob3.hel()
ob2=tt(ob1,ob3)
ob2.display()
方法 1 和 2 的区别是什么? 我的要求是我有几个类:config.py、log.py、analyze.py、HTTPrequest.py、request.py、attack.py
以上所有类都需要来自 config.py 和 log.py 的类变量值。 而且, analyze.py 需要 request.py 和 HTTPRequest 中的值,py 和 attack.y 需要 request.py 中的值。
谁能帮助我如何继承多级或多级或 usecomposition 或仅传递类对象以及如何?
【问题讨论】:
标签: python inheritance composition