Python class的继承方法代码实例


Posted in Python onFebruary 14, 2020

这篇文章主要介绍了Python class的继承方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

class parent(object):
   
  def implicit(self):
    print("Parent implicit()")
  def override(self):
    print("Parent override()")
  def altered(self):
    print("Parent altered()")
     
class child(parent):
   
  def override(self):
    print("Child override()")
  def altered(self):
    print("Child,Before Parent altered()")
    super(child,self).altered()
    print("Child,After Parent altered()")
     
dad=parent()
son=child()
 
dad.implicit()
son.implicit()
 
dad.override()
son.override()
 
dad.altered()
son.altered()

运行结果:

Parent implicit()
Parent implicit()
Parent override()
Child override()
Parent altered()
Child,Before Parent altered()
Parent altered()
Child,After Parent altered()

还可以写成:

class parent():
   
  def implicit(self):
    print("Parent implicit()")
  def override(self):
    print("Parent override()")
  def altered(self):
    print("Parent altered()")
     
class child(parent):
   
  def __init__(self):
    self.parent =parent()
     
  def implicit(self):
    self.parent.implicit()
     
  def override(self):
    print("Child override()")
     
  def altered(self):
    print("Child,Before Parent altered()")
    super(child,self).altered()
    print("Child,After Parent altered()")
     
dad=parent()
son=child()
 
dad.implicit()
son.implicit()
 
dad.override()
son.override()
 
dad.altered()
son.altered()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
举例讲解Python中装饰器的用法
Apr 27 Python
详解python 发送邮件实例代码
Dec 22 Python
python使用xlrd与xlwt对excel的读写和格式设定
Jan 21 Python
Python中常用的8种字符串操作方法
May 06 Python
window环境pip切换国内源(pip安装异常缓慢的问题)
Dec 31 Python
opencv python在视屏上截图功能的实现
Mar 05 Python
python实现图片横向和纵向拼接
Mar 05 Python
Python figure参数及subplot子图绘制代码
Apr 18 Python
Python2.x与3​​.x版本有哪些区别
Jul 09 Python
matplotlib更改窗口图标的方法示例
Feb 03 Python
python 实现定时任务的四种方式
Apr 01 Python
教你用python实现一个无界面的小型图书管理系统
May 21 Python
python super函数使用方法详解
Feb 14 #Python
python字符串,元组,列表,字典互转代码实例详解
Feb 14 #Python
python集成开发环境配置(pycharm)
Feb 14 #Python
基于python-pptx库中文文档及使用详解
Feb 14 #Python
python pptx复制指定页的ppt教程
Feb 14 #Python
打包PyQt5应用时的注意事项
Feb 14 #Python
如何使用Python抓取网页tag操作
Feb 14 #Python
You might like
PHP+Tidy-完美的XHTML纠错+过滤
2007/04/10 PHP
PHP代码审核的详细介绍
2013/06/13 PHP
IIS+fastcgi下PHP运行超时问题的解决办法详解
2013/06/20 PHP
php cli换行示例
2014/04/22 PHP
php 魔术方法详解
2014/11/11 PHP
使用php-timeit估计php函数的执行时间
2015/09/06 PHP
Javascript和Ajax中文乱码吐血版解决方案
2009/12/21 Javascript
JQuery index()方法使用代码
2010/06/02 Javascript
基于Jquery的文字滚动跑马灯插件(一个页面多个滚动区)
2010/07/26 Javascript
jquery自定义属性(类型/属性值)
2013/05/21 Javascript
JS 实现点击a标签的时候让其背景更换
2013/10/15 Javascript
JS判断文本框内容改变事件的简单实例
2014/03/07 Javascript
jquery获取颜色在ie和ff下的区别示例介绍
2014/03/28 Javascript
使用javascript实现Iframe自适应高度
2014/12/24 Javascript
javascript继承的六大模式小结
2015/04/13 Javascript
JavaScript ES6的新特性使用新方法定义Class
2016/06/28 Javascript
jQuery Easyui学习教程之实现datagrid在没有数据时显示相关提示内容
2016/07/09 Javascript
jQuery Easyui加载表格出错时在表格中间显示自定义的提示内容
2016/12/08 Javascript
js实现常见的工具条效果
2017/03/02 Javascript
微信JSAPI Ticket接口签名详解
2020/06/28 Javascript
Bootstrap一款超好用的前端框架
2017/09/25 Javascript
vue项目刷新当前页面的三种方法
2018/12/04 Javascript
js前端面试之同步与异步问题详解
2019/04/03 Javascript
webpack+express实现文件精确缓存的示例代码
2020/06/11 Javascript
scrapy自定义pipeline类实现将采集数据保存到mongodb的方法
2015/04/16 Python
Python的Flask框架中SQLAlchemy使用时的乱码问题解决
2015/11/07 Python
python读取视频流提取视频帧的两种方法
2020/10/22 Python
Pandas过滤dataframe中包含特定字符串的数据方法
2018/11/07 Python
使用python实现滑动验证码功能
2019/08/05 Python
Python 在局部变量域中执行代码
2020/08/07 Python
KIKO MILANO荷兰网上商店:意大利专业化妆品品牌
2017/05/12 全球购物
Java TransactionAPI (JTA) 主要包含几部分
2012/12/07 面试题
顺丰快递Java软件工程师面试题
2015/07/31 面试题
幼儿园教师自我鉴定
2014/03/20 职场文书
终止劳动合同通知书
2015/04/16 职场文书
2015年节能降耗工作总结
2015/05/22 职场文书