python super()函数
super()函数是用来调用父类(超类)的一个方法
super()的语法:
python 2 的用法:
super(Class, self).xxx # class是子类的名称
class A(object):
pass class B(a):
def add(self, x):
super(B,self).add(x)
python 3用法:
super().xxx
class A:
pass class B(A):
def add(self, x):
super().add(x) 实例:
# _*_ coding:utf-8 _*_ class FooParent:
def __init__(self):
self.parent = 'I\'m the parent'
print('parent') def bar(self, message):
print('%s from Parent' % message) class FooChild(FooParent):
def __init__(self):
super(FooChild, self).__init__() # 把FooChild对象转化成FooParent的对象,调用父类里的__init__(self),会输出‘parent'
self.child = 'this is child'
print('child') # 上一步调用结束后,打印‘child' def bar(self, message):
super(FooChild, self).bar(message) # 调用父类里的bar函数
print('child bar function')
print(self.parent) # 打印父类的属性parent
print(self.child)
if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld') 输出结果
parent
child
HelloWorld from Parent
child bar function
I'm the parent
this is child
python super()函数的更多相关文章
- Python super() 函数的概念和例子
概念: super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO).重 ...
- Python super() 函数
super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果重定义某个方法,该方法会覆盖父类的同名方法,但有时 ...
- python super()函数详解
引言: 在类的多继承使用场景中,重写父类的方法时,可能会考虑到需要重新调用父类的方法,所以super()函数就是比较使用也很必要的解决方法: 文章来源: http://www.cnblogs.com/ ...
- python super()函数:调用父类的构造方法
python子类会继承父类所有的类属性和类方法.严格来说,类的构造方法其实就是实例方法,因此,父类的构造方法,子类同样会继承. 我们知道,python是一门支持多继承的面向对象编程语言,如果子类继承的 ...
- Python setattr() 函数 ,Python super() 函数: Python 内置函数 Python 内置函数
描述 setattr 函数对应函数 getatt(),用于设置属性值,该属性必须存在. 语法 setattr 语法: setattr(object, name, value) 参数 object -- ...
- Python中super函数的用法
之前看python文档的时候发现许多单继承类也用了super()来申明父类,那么这样做有何意义? 从python官网文档对于super的介绍来看,其作用为返回一个代理对象作为代表调用父类或亲类方法.( ...
- python之super()函数
python之super()函数 python的构造器奇特, 使用魔方. 构造器内对基类对象的初始化同样也很奇特, 奇特到没有半点优雅! 在构造器中使用super(class, instance)返回 ...
- 由Python的super()函数想到的
python-super *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !im ...
- Python面试题之Super函数
这是个高大上的函数,在python装13手册里面介绍过多使用可显得自己是高手 23333. 但其实他还是很重要的. 简单说, super函数是调用下一个父类(超类)并返回该父类实例的方法. 这里的下一 ...
- python 面向对象十一 super函数
python 面向对象十一 super函数 super函数用来解决钻石继承. 一.python的继承以及调用父类成员 父类: class Base(object): def __init__(se ...
随机推荐
- React 新 Context API 在前端状态管理的实践
本文转载至:今日头条技术博客 众所周知,React的单向数据流模式导致状态只能一级一级的由父组件传递到子组件,在大中型应用中较为繁琐不好管理,通常我们需要使用Redux来帮助我们进行管理,然而随着Re ...
- 20165214 实验三 敏捷开发与XP实践
一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:朱文远 学号:20165214 指导教师:娄嘉鹏 实验日期:2018年4月28日 实验时间:15:35 - 17:15 实验序号:三 ...
- Arduino-汉王PM2.5检测模组B1
汉王PM2.5检测模组B1,接入Arduino,使用I2C1602显示屏显示 #include <Arduino.h> #include <Wire.h> #include & ...
- table 表头固定 thead固定. 1) 使用jquery.freezeheader.js
方法一: 使用jquery.freezeheader.js 固定表头: 1-: 初始化: <!DOCTYPE html> <html lang="en"> ...
- Spring Boot项目中使用Swagger2
Swagger2是一款restful接口文档在线生成和在线接口调试工具,Swagger2在Swagger1.x版本的基础上做了些改进,下面是在一个Spring Boot项目中引入Swagger2的简要 ...
- “Cannot make a static reference to the non-static method”处理方法
报错原文:Cannot make a static reference to the non-static method maxArea(Shape[]) from the type ShapeTes ...
- Eclipse_设置_01_自动提示
- Popover 弹出框 设置top,显示有时是向下的,解决方式
参数里面有个popper-options,官网给的值是{boundariesElement: 'body', gpuAcceleration: false },将这个加上问题就解决了.
- 九度OJ1205题-递归求解问题
题目1205:N阶楼梯上楼问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:5887 解决:2446 题目描述: N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用 ...
- Unity 3D光源-Directional平行光/逆光效果,光晕详解、教程
Unity4大光源之平行光 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...