Python super() 函数的概念和例子
概念:
super() 函数是用于调用父类(超类)的一个方法。
super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。
格式:
super(type[, object-or-type])
- type -- 类。
- object-or-type -- 类,一般是 self
- Python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :
简单实例:
可以说明supper的概念和使用方法.
class Base(object):
def __init__(self):
print "Base created" class ChildA(Base):
def __init__(self):
Base.__init__(self) class ChildB(Base):
def __init__(self):
super(ChildB, self).__init__() ChildA()
ChildB()
输出结果:
Base created
Base created
高级实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*- class FooParent(object):
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) 首先找到 FooChild 的父类(就是类 FooParent),然后把类B的对象 FooChild 转换为类 FooParent 的对象
super(FooChild,self).__init__()
print ('Child') def bar(self,message):
super(FooChild, self).bar(message)
print ('Child bar fuction')
print (self.parent) if __name__ == '__main__':
fooChild = FooChild()
fooChild.bar('HelloWorld')
输出结果:
Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.
Python super() 函数的概念和例子的更多相关文章
- 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装13手册里面介绍过多使用可显得自己是高手 23333. 但其实他还是很重要的. 简单说, super函数是调用下一个父类(超类)并返回该父类实例的方法. 这里的下一 ...
- 由Python的super()函数想到的
python-super *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !im ...
- python学习之路---day20--面向对象--多继承和super() 函数
一:python多继承 python多继承中,当一个类继承了多个父类时候,这个类拥有多个父类的所欲非私有的属性 l例子: class A: pass class B(A): pass class C( ...
- python函数纯概念汇总(一)
在使用python的时候由于前期基本概念没有分清楚,所以需要重新归纳汇总学一学. 一.什么是函数: 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很 ...
- 认识python中的super函数
需求分析 在类继承中,存在这么一种情况: class Human(object): def Move(self): print("我会走路...") class Man(Human ...
随机推荐
- canvas的使用方法
了解canvas:canvas标签是用作图形绘制,但是通过js脚本来实现的,canvas标签其实只是一个容器 ,最终实现绘制功能肯定是通过js脚本实现. 首先肯定要定义一个canvas标签当做容器 & ...
- Numpy安装
Python官网上的发行版是不包含Numpy模块的. 1.使用已有的发起行版本 对于许多用户,尤其是在Windows上,最简单的方法就是下载以下的Python发行版,他们包涵了所有的关键包(包括Num ...
- Fair Candy Swap LT888
Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...
- 树莓派RaspBerry账户初始化设定
1 第一次安装系统进入后默认账户是pi/raspberry root账户是默认锁定的 sudo passwd root 设置root账户密码 sudo passwd --unlock root 开启 ...
- 无法获得锁 /var/lib/dpkg/lock - open
关于 apt-get update 更新的时候出现 无法获得锁 /var/lib/dpkg/lock - open 解决 直接删除这个锁文件即可: sudo rm /var/lib/dpkg/lock ...
- 1030 Travel Plan Dijkstra+dfs
和1018思路如出一辙,先求最短路径,再dfs遍历 #include <iostream> #include <cstdio> #include <vector> ...
- 移动端--用PX为单位+JS框架 实现页面布局
一:大家先下载metahandler.js 二:准备一个用px实现的移动页面(宽度固定死的页面),引入metahandler.js框架 1.视口设置 width=640,是根据psd图来设置,有多宽设 ...
- web项目中对post请求乱码处理
<filter> <filter-name>characterEncoding</filter-name> <filter-class>org.spri ...
- Oracle简单查询实例
--查询不重复的职位 select distinct job from emp; --查询年薪,起别名,别名不要用单引号括起来 as nianxin from emp sal; --以这样的形式显示具 ...
- Scala知识点汇总
Scala数组小结 1.定长数组 定长数组:指长度不可变的数组Array.第一种方式:先声明一个数组,后初始化该数组: scala> val array = new Array[Double]( ...