深入浅析python继承问题
更新时间:2016年05月29日 14:59:17 投稿:mrr
这篇文章主要介绍了深入浅析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)
然后代码的运行结果如下:
F:\python_code\test>python class_init.py init in c1 init in p1 f1 in p2 f1 in p2 11 22 2
关于的代码的运行过程,我有以下的疑问,我在 c1 的 __init__ 函数中会调用到p1.__init__(),然后会每次都是运行 c1.f1() 函数,没有运行 p1.f1() 的函数,在 p1 运行的 f1(),怎么也是 c1.f1()。为什么?
原因分析:
p1.__init__(self,a,b)
这行代码中的self是c1的对象。所以传给p1里面的self也就是c1的呀。
以上内容所述通过代码给大家介绍了python继承问题,希望对大家有所帮助!
相关文章
python构造icmp echo请求和实现网络探测器功能代码分享
本文分享了二个python示例,python构造icmp echo请求、实现网络探测器功能代码,类似nmap功能2014-01-01
np.where()[0] 和 np.where()[1]的具体使用
这篇文章主要介绍了np.where()[0] 和 np.where()[1]的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-03-03
Python 处理 Pandas DataFrame 中的行和列
这篇文章主要介绍了Python处理Pandas DataFrame中的行和列,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下2022-09-09


最新评论