Python 继承,重写,super()调用父类方法操作示例
作者:houyanhua1 时间:2022-08-09 13:22:13
本文实例讲述了Python 继承,重写,super()调用父类方法操作。分享给大家供大家参考,具体如下:
demo.py(继承,重写,super):
# 父类
class Dog:
def bark(self):
print("汪汪叫")
# 子类 继承
class XiaoTianQuan(Dog):
def fly(self):
print("我会飞")
# 可以重写父类中的同名方法
def bark(self):
# super().父类方法名 调用父类中的方法 (第一种方式)(推荐)
super().bark()
# 父类名.方法(self) 调用父类中的方法 (第二种方式,python2.x)(不推荐,父类名修改后,此处也得改)
Dog.bark(self)
# 注意:如果使用子类名调用方法,可能会出现递归调用 -- 死循环!
# XiaoTianQuan.bark(self) # 会产生死循环
# 针对子类特有的需求,进行扩展
print("神一样的叫唤...")
xtq = XiaoTianQuan()
xtq.bark()
运行结果:
汪汪叫
汪汪叫
神一样的叫唤...
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/houyanhua1/article/details/84074597
标签:Python,继承,重写,super
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python脚本开发漏洞的批量搜索与利用(GlassFish 任意文件读取)
2023-01-30 20:08:36
![](https://img.aspxhome.com/file/2023/1/93611_0s.png)
Python实现图像增强
2022-07-08 10:50:50
![](https://img.aspxhome.com/file/2023/9/80549_0s.jpg)
通过Python来使用七牛云存储的方法详解
2022-09-13 19:56:36
![](https://img.aspxhome.com/file/2023/2/64602_0s.png)
python实现电子词典
2021-12-19 08:29:03
![](https://img.aspxhome.com/file/2023/2/100752_0s.jpg)
pandas 使用apply同时处理两列数据的方法
2021-09-27 07:35:30
CentOS 6.6服务器编译安装lnmp(Nginx1.6.2+MySQL5.6.21+PHP5.6.3)
2023-11-15 06:40:50
![](https://img.aspxhome.com/file/2023/6/64186_0s.jpg)
js实现千分符和保留几位小数的简单实例
2023-08-21 10:03:26
Python读取HTML中的canvas并且以图片形式存入Word文档
2022-09-03 04:50:33
![](https://img.aspxhome.com/file/2023/0/89430_0s.png)
pytorch 中forward 的用法与解释说明
2021-08-10 04:14:44
Python实现注册登录系统
2021-10-21 20:01:05
![](https://img.aspxhome.com/file/2023/4/68324_0s.jpg)
Python super( )函数用法总结
2022-07-09 23:23:10
Oracle逗号分隔列转行实现方法
2011-01-04 20:13:00
最大限度优化你的Asp性能
2007-10-01 18:04:00
轻松接触SQL Server 2000实例的命名规则
2009-01-23 13:44:00
如何修复MySQL数据库表
2009-03-20 13:24:00
不要忽略了颜色的可用性
2009-03-05 18:19:00
![](https://img.aspxhome.com/file/UploadPic/20093/5/01-24s.jpg)
python算法深入理解风控中的KS原理
2021-01-12 21:27:37
![](https://img.aspxhome.com/file/2023/6/101566_0s.png)
微信小程序利用Canvas绘制图片和竖排文字详解
2023-08-20 02:35:43
![](https://img.aspxhome.com/file/2023/2/56022_0s.jpg)
opencv-python 提取sift特征并匹配的实例
2022-01-25 03:43:18
Python之列表的插入&替换修改方法
2023-04-15 00:38:27