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记录程序运行时间的三种方法
Jul 14 Python
Python 快速实现CLI 应用程序的脚手架
Dec 05 Python
Python3编码问题 Unicode utf-8 bytes互转方法
Oct 26 Python
python实现逐个读取txt字符并修改
Dec 24 Python
在python中使用with打开多个文件的方法
Jan 07 Python
Python人脸识别第三方库face_recognition接口说明文档
May 03 Python
Django框架实现的普通登录案例【使用POST方法】
May 15 Python
python中有关时间日期格式转换问题
Dec 25 Python
Python将字典转换为XML的方法
Aug 01 Python
python爬虫使用scrapy注意事项
Nov 23 Python
如何用 Python 处理不平衡数据集
Jan 04 Python
python实现对doc、txt、xls等文档的读写操作
Apr 02 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
英雄试炼之肉山谷—引领RPG新潮流
2020/04/20 DOTA
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
2014/11/06 PHP
Yii框架关联查询with用法分析
2014/12/02 PHP
PHP实现数组array转换成xml的方法
2016/07/19 PHP
PHP流Streams、包装器wrapper概念与用法实例详解
2017/11/17 PHP
In Javascript Class, how to call the prototype method.(three method)
2007/01/09 Javascript
Uglifyjs(JS代码优化工具)入门 安装使用
2020/04/13 Javascript
如何在一个页面显示多个百度地图
2013/04/07 Javascript
ext combobox动态加载数据库数据(附前后台)
2014/06/17 Javascript
简单谈谈node.js 版本控制 nvm和 n
2015/10/15 Javascript
jQuery插件Validate实现自定义校验结果样式
2016/01/18 Javascript
BootStrap iCheck插件全选与获取value值的解决方法
2016/08/24 Javascript
解决webpack打包速度慢的解决办法汇总
2017/07/06 Javascript
Mongoose中document与object的区别示例详解
2017/09/18 Javascript
基于Vue的移动端图片裁剪组件功能
2017/11/28 Javascript
JavaScript+Canvas实现彩色图片转换成黑白图片的方法分析
2018/07/31 Javascript
vue实现数字滚动效果
2020/06/29 Javascript
Vue+Vant 图片上传加显示的案例
2020/11/03 Javascript
Python入门篇之条件、循环
2014/10/17 Python
为Python的Tornado框架配置使用Jinja2模板引擎的方法
2016/06/30 Python
Python 基础知识之字符串处理
2017/01/06 Python
python九九乘法表的实例
2017/09/26 Python
python+influxdb+shell编写区域网络状况表
2018/07/27 Python
python2.7和NLTK安装详细教程
2018/09/19 Python
python 切换root 执行命令的方法
2019/01/19 Python
python生成器/yield协程/gevent写简单的图片下载器功能示例
2019/10/28 Python
Python列表切片常用操作实例解析
2019/12/16 Python
使用matplotlib的pyplot模块绘图的实现示例
2020/07/12 Python
Python使用xpath实现图片爬取
2020/09/16 Python
美国最大的骑马用品零售商:HorseLoverZ
2017/01/12 全球购物
美国宠物护理专家:Revival Animal Health
2020/01/05 全球购物
教师业务学习制度
2014/01/25 职场文书
十佳中学生事迹材料
2014/06/02 职场文书
恋恋笔记本观后感
2015/06/16 职场文书
大学军训通讯稿(2016最新版)
2015/12/21 职场文书
Oracle11g R2 安装教程完整版
2021/06/04 Oracle