python class继承
https://blog.csdn.net/brucewong0516/article/details/79121179
类继承:
class SubClassName(parentClass,[,parentClass2,..]):
class_suite
实现继承之后,子类将继承父类的属性,也可以使用内建函数insubclass()来判断一个类是不是另一个类的子孙类
issubclass(Child, Parent),其中,child和parent都是class,child继承parent
class Parent(object):
'''
parent class
'''
numList = []
def numdiff(self, a, b):
return a-b class Child(Parent):
pass c = Child()
# subclass will inherit attributes from parent class
#子类继承父类的属性
Child.numList.extend(range(10))
print(Child.numList) print("77 - 2 =", c.numdiff(77, 2)) # built-in function issubclass()
print(issubclass(Child, Parent))
print(issubclass(Child, object)) # __bases__ can show all the parent classes
#bases属性查看父类
print('the bases are:',Child.__bases__) # doc string will not be inherited
#doc属性不会被继承
print(Parent.__doc__)
print(Child.__doc__)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
77 - 2 = 75
True
True
the bases are: (<class '__main__.Parent'>,) parent class None
super的使用详解
- super主要来调用父类方法来显示调用父类,在子类中,一般会定义与父类相同的属性(数据属性,方法),从而来实现子类特有的行为。也就是说,子类会继承父类的所有的属性和方法,子类也可以覆盖父类同名的属性和方法。
class Parent(object):
Value = "Hi, Parent value"
def fun(self):
print("This is from Parent")
#定义子类,继承父类
class Child(Parent):
Value = "Hi, Child value"
def ffun(self):
print("This is from Child")
c = Child()
c.fun()
c.ffun()
print(Child.Value)
This is from Parent
This is from Child
Hi, Child value
但是,有时候可能需要在子类中访问父类的一些属性,可以通过父类名直接访问父类的属性,当调用父类的方法是,需要将”self”显示的传递进去的方式:
class Parent(object):
Value = "Hi, Parent value"
def fun(self):
print("This is from Parent") class Child(Parent):
Value = "Hi, Child value"
def fun(self):
print("This is from Child")
Parent.fun(self) #调用父类Parent的fun函数方法 c = Child()
c.fun()
This is from Child
This is from Parent #实例化子类Child的fun函数时,首先会打印上条的语句,再次调用父类的fun函数方法
这种方式有一个不好的地方就是,需要经父类名硬编码到子类中,为了解决这个问题,可以使用Python中的super关键字:
class Parent(object):
Value = "Hi, Parent value"
def fun(self):
print("This is from Parent") class Child(Parent):
Value = "Hi, Child value"
def fun(self):
print("This is from Child")
#Parent.fun(self)
super(Child,self).fun() #相当于用super的方法与上一调用父类的语句置换 c = Child()
c.fun()
This is from Child
This is from Parent #实例化子类Child的fun函数时,首先会打印上条的语句,再次调用父类的fun函数方法
_ _ doc _ _ :类的文档字符串
_ _ bases _ _:类的所有父类组成的元组
_ _ dict _ _:类的属性组成的字典
_ _ module _ _:类所属的模块
_ _ class _ _:类对象的类型
copy from https://blog.csdn.net/brucewong0516/article/details/79121179
python class继承的更多相关文章
- sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO
sqlalchemy mark-deleted 和 python 多继承下的方法解析顺序 MRO 今天在弄一个 sqlalchemy 的数据库基类的时候,遇到了跟多继承相关的一个小问题,因此顺便看了一 ...
- python基础——继承和多态
python基础——继承和多态 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类 ...
- [修]python普通继承方式和super继承方式
[转]python普通继承方式和super继承方式 原文出自:http://www.360doc.com/content/13/0306/15/9934052_269664772.shtml 原文的错 ...
- Python进阶-继承中的MRO与super
Python进阶-继承中的MRO与super 写在前面 如非特别说明,下文均基于Python3 摘要 本文讲述Python继承关系中如何通过super()调用"父类"方法,supe ...
- python基础——继承实现的原理
python基础--继承实现的原理 1 继承顺序 class A(object): def test(self): print('from A') class B(A): def test(self) ...
- python基础——继承与派生、组合
python基础--继承与派生 1 什么是继承: 继承是一种创建新的类的方式,在python中,新建的类可以继承自一个或者多个父类,原始类成为基类或超累,新建的类成为派生类或子类 1.1 继承分为:单 ...
- python 类继承演示范例的代码
把做工程过程重要的代码片段备份一次,下面的资料是关于python 类继承演示范例的代码. # a simple example of a class inheritance # tested with ...
- [py]python的继承体系-源码目录结构
python3安装目录 pip install virtualenv pip install virtualenvwrapper pip install virtualenvwrapper-win m ...
- [py]python的继承体系
python的继承体系 python中一切皆对象 随着类的定义而开辟执行 class Foo(object): print 'Loading...' spam = 'eggs' print 'Done ...
- Python多继承解析顺序的C3线性算法流程解析
Python多继承MRO 在Python2.1中,采用了经典类,使用深度优先算法解析. Python2.2中,引入了新式类,使用深度优先算法和广度优先算法. 在Python2.3以后的版本中,经典类和 ...
随机推荐
- python2和python3 安装pip冲突问题
系统:win10 问题:安装python2和Python3后 在cmd 中 pip和python命令会冲突 原因:先安装的python3,python3会自动配置path安装pip,pip和pytho ...
- 19.3.20 cmd操作:1.dir查看当前文件夹内的文件;2.alt+space+c关闭cmd窗口
cmd操作记录: 1.dir:查看当前文件夹内的所有文件: 2.alt+space+c:关闭打开的cmd窗口:
- jquery弹窗插件layer:layer.layui.com
这两天在做抽奖转盘功能,浏览器自带的alert弹出框太low,本人又基本不会前端, 于是借鉴前人用fancybox插件做的效果 结果没看懂其写法(http://www.0101shop.com/goo ...
- Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.
运行代码时,一直报错: 经过查询后才知道,vue模板只能有一个跟对象 我是这样写的 最后修改为 就可以正常运行了
- 报文分析3、ICMP协议的头结构
ICMP协议的头结构 类型(8位) 代码(8位) 校验和(8位) 类型或者代码 (1)类型:一个8位类型字段,表示ICMP数据包类型. (2)代码:一个8位代码域,表示指定类型中的一个功能.如果一 ...
- [strongswan][autoconf][automake][cento] 在CentOS上编译strongswan git源码时遇到的autoconf问题
编译strongswan的git源码问题 1. 概述 首先,我们想要通过源码编译strongswan.当满足以下条件时,通常你会遇见此问题: 源码时通过git clone的得来的,而不是官网下载的源码 ...
- Mac苹果电脑没有声音怎么办
有时候 Mac 从睡眠状态恢复之后没有声音,这是 Mac OS X 系统的一个 Bug.这是因为 Mac OS X 的核心音频守护进程「coreaudiod」出了问题,虽然简单的重启电脑就能解决,但是 ...
- vim模式下报错E37: No write since last change (add ! to override)
故障现象: 使用vim修改文件报错,系统提示如下: E37: No write since last change (add ! to override) 故障原因: 文件为只读文件,无法修改. 解决 ...
- photoshop 修改pdf文件并保存为pdf
1.CTRL + O 打开要编辑的pdf文件 按住shift 选中每一页,点击确定. pdf文档每一页以一个psd文件显示在工作区, 分别进行修改, 2.批量修改同一个元素(比如加个图标) 在一页 ...
- 接口测试工具-Jmeter使用笔记(八:模拟OAuth2.0协议简化模式的请求)
背景 博主的主要工作是测试API,目前已经用Jmeter+Jenkins实现了项目中的接口自动化测试流程.但是马上要接手的项目,API应用的是OAuth2.0协议授权,并且采用的是简化模式(impli ...