|
发表于 2023-8-26 21:11:24
|
显示全部楼层
本帖最后由 ww185912 于 2023-8-26 21:12 编辑
自己看看,以后可以用这种方式,看程序是怎么运行的
- >>> class A:
- ... def __init__(self):
- ... self.n = 2
- ... print(self,self.n)
- ... def add(self,m):
- ... self.n += m
- ... print(self,self.n,m)
- ...
- >>> class B(A):
- ... def __init__(self):
- ... self.n = 3
- ... print("B",self,self.n)
- ... def add(self,m):
- ... super().add(m)
- ... self.n += 3
- ... print("B",self,self.n,m)
- ...
- >>> b = B()
- B <__main__.B object at 0x000002C14D286E50> 3
- >>> b.add(2)
- <__main__.B object at 0x000002C14D286E50> 5 2
- B <__main__.B object at 0x000002C14D286E50> 8 2
- >>> b.n
- 8
复制代码
注意,__init__函数只有在实例化时候才会执行 |
|