python继承问题_深入浅析python继承问题
有如下的代碼:
class p1:
def __init__(self,a,b):
print("init in p1")
self.a1=a
self.b1=b
self.f1()
def f1(self):
print("f1 in p1")
class c1(p1):
def __init__(self,a,b,c=2):
print("init in c1")
p1.__init__(self,a,b)
self.c1=c
self.f1()
def f1(self):
print("f1 in p2")
class c2(c1):
pass
c=c2(11,22)
print(c.a1)
print(c.b1)
print(c.c1)
然后代碼的運(yùn)行結(jié)果如下:
F:\python_code\test>python class_init.py
init in c1
init in p1
f1 in p2
f1 in p2
11
22
2
關(guān)于的代碼的運(yùn)行過(guò)程,我有以下的疑問(wèn),我在 c1 的 __init__ 函數(shù)中會(huì)調(diào)用到p1.__init__(),然后會(huì)每次都是運(yùn)行 c1.f1() 函數(shù),沒(méi)有運(yùn)行 p1.f1() 的函數(shù),在 p1 運(yùn)行的 f1(),怎么也是 c1.f1()。為什么?
原因分析:
p1.__init__(self,a,b)
這行代碼中的self是c1的對(duì)象。所以傳給p1里面的self也就是c1的呀。
以上內(nèi)容所述通過(guò)代碼給大家介紹了python繼承問(wèn)題,希望對(duì)大家有所幫助!
希望與廣大網(wǎng)友互動(dòng)??
點(diǎn)此進(jìn)行留言吧!
總結(jié)
以上是生活随笔為你收集整理的python继承问题_深入浅析python继承问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python爬取12306_python
- 下一篇: emacs python plugin_