python 类继承与子类实例初始化
From: https://blog.csdn.net/cs0301lm/article/details/6002504?utm_source=blogxgwz4
[ 先贴参考书籍原文(中文英文对照)]
__init__方法介绍:
If a base class has an __init__() method the derived class's __init__() method must explicitly call it to ensure proper initialization of the base class part of the instance; for example: "BaseClass.__init__(self, [args...])"
As a special contraint on constructors, no value may be returned; doing so will cause a TypeError to be raised at runtime.
如果其基类也具有__init__(), 必须显式地在__init__()调用它, 以保证能够适当地初始化它的基类部分;例如: "BaseClass.__init__(self, [args...])"作为构造器的特殊情况, 它没有值被返回, 如果返回某个值, 会在运行时抛出异常TypeError.
1.子类定义了__init__方法时若未显示调用基类__init__方法,python不会帮你调用,解释运行都Ok
class A():
def __init__(self):
print 'a'
class B(A):
def __init__(self):
print 'b'
if __name__=='__main__':
b=B()
>>>
b
2.子类未定义__init__方法时,python会自动帮你调用首个基类的__init__方法,注意是首个。即:子类继承自多个基类时,只有第一个基类的__init__方法会被调用到:
class A:
def __init__(self):
print 'a'
class B:
def __init__(self):
print 'b'
class C(B):
def __init__(self):
print 'c'
pass
class D1(A,B,C):
pass
class D2(B,A,C):
pass
class D3(C,B,A):
pass
if(__name__=='__main__'):
print 'd1------->:'
d1=D1()
print 'd2------->:'
d2=D2()
print 'd3------->:'
d3=D3()
>>>
d1------->:
a
d2------->:
b
d3------->:
c
3)基类未定义__init__方法时,若此时子类显示调用基类__init__方法时,python向上超找基类的基类的__init__方法并调用,实质同2
class A:
def __init__(self):
print 'a'
class B:
def __init__(self):
print 'b'
class C1(B,A):
pass
class C2(A,B):
pass
class D1(C1):
def __init__(self):
C1.__init__(self)
class D2(C2):
def __init__(self):
C2.__init__(self)
if(__name__=='__main__'):
print 'd1------->:'
d1=D1()
print 'd2------->:'
d2=D2()
>>>
d1------->:
b
d2------->:
a
---------------------
python 类继承与子类实例初始化的更多相关文章
- python 类继承演示范例的代码
把做工程过程重要的代码片段备份一次,下面的资料是关于python 类继承演示范例的代码. # a simple example of a class inheritance # tested with ...
- python 类的定义 实例化 实例完后初始化
先来看看 类的__init__, 类的__new__ , 元类的__new__的执行顺序 class TMetaclass(type): def __new__(cls,name,bases,attr ...
- python类继承
面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过 继承 机制.继承完全可以理解成类之间的 类型和子类型 关系. 假设你想要写一个程序来记录学校之中的教师和学生情况.他们有一些 ...
- python类继承的重写和super
给已经存在的类添加新的行为,继承是非常好的实现方式.但是如果要改变行为呢?比如在Python继承扩展内置类,我们的contact类只允许一个名字和一个邮箱,但是如果要对某些人增加电话号码呢?这里可以通 ...
- 第7.7节 案例详解:Python类继承机制
本节实现一个类继承的小程序,下面一边结合代码一边介绍相关继承的知识.例子以车.汽车为例,车为父类.汽车为子类. 一. 定义父类Vehicle class Vehicle(): def __ ...
- Python 类继承,__bases__, __mro__, super
Python是面向对象的编程语言,也支持类继承. >>> class Base: ... pass ... >>> class Derived(Base): ... ...
- Python类继承(转发)
目录 一.概述 二.类的继承 2.1 继承的定义 2.2 构造函数的继承 2.3 子类对父类方法的重写 三.类继承的事例 回到顶部 一.概述 面向对象编程 (OOP) 语言的一个主要功能就是“继承”. ...
- Java类继承关系中的初始化顺序
Java类初始化的顺序经常让人犯迷糊,现在本文尝试着从JVM的角度,对Java非继承和继承关系中类的初始化顺序进行试验,尝试给出JVM角度的解释. 非继承关系中的初始化顺序 对于非继承关系,主类Ini ...
- python 类 - 继承
继承 什么是继承? 编写类时,并非总要从空白开始.如果要编写的类是另一个现成类的特殊版本,可使用继承. 一个类继承另一个类时,将自动获得另一个类的所有属性和方法.现有的类称为父类,而新类称为子类. 子 ...
随机推荐
- 【摄像头】Global Shutter(全局快门)与Rolling Shutter(卷帘快门)的区别与比较
由于红外补光灯的爆闪,所以一般DMS会用global shutter的sensor,而不是rolling shutter的. 参考 1. Global Shutter(全局快门)与Rolling Sh ...
- C++中输出字符到文本文档
#include <iostream> #include <fstream> //ofstream类的头文件 using namespace std; int main() { ...
- [LeetCode&Python] Problem 771: Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- centos7 firewalld基本使用
firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl disable ...
- Python——dict(自定义类作key)
Python的dict要求key为不可变数据类型,通常采用str或int,但在某些应用场景下,需要采用自定义类型对象作key, 此时的自定义类需要实现两个特殊方法:__hash__.__eq__,用于 ...
- uboot2014.10移植(一)
最新有点时间,所以想折腾点东西,于是拿起了几年前的TQ2440玩玩,下载了uboot2014.10版本,准备移植到板子上去,没想到折腾环境都折腾了一下午. 1.工具链安装 我的工具链直接用命令安装的: ...
- String的intern()方法详解
https://blog.csdn.net/soonfly/article/details/70147205 :图解 https://blog.csdn.net/wjzhang5514/articl ...
- 对中文进行MD5加密的注意事项(Java版,编码问题)
http://blog.csdn.net/tongdao/article/details/20690187 在工作中需要和第三方进行Http通信,在通信内容中有几个参数涉及到了中文.自己在进行MD5加 ...
- MySQL--派生表临时结果集中的AutoKey
在某些场景中,需要对派生表生成临时结果集进行materialized,如果该临时结果集中包含索引键,那么查询有可能通过该索引键来进行优化. 如对下面查询: SELECT T2.purpose_code ...
- Benchmarking Zeebe: An Intro to How Zeebe Scales Horizontally and How We Measure It
Written by Felix Müller and Mike Winters on Jun 12 2018 in the Inside Zeebe category. In the past fe ...