python-继承,父类,子类
class Spell(object):
def __init__(self, incantation, name):
self.name = name
self.incantation = incantation def __str__(self):
return self.name + ' ' + self.incantation + '\n' + self.getDescription() def getDescription(self):
return 'No description' def execute(self):
print(self.incantation) class Accio(Spell):
def __init__(self):
Spell.__init__(self, 'Accio', 'Summoning Charm') class Confundo(Spell):
def __init__(self):
Spell.__init__(self, 'Confundo', 'Confundus Charm') def getDescription(self):
return 'Causes the victim to become confused and befuddled.' def studySpell(spell):
print(spell) spell = Accio()
spell.execute()
studySpell(spell)
studySpell(Confundo())
输出:
Accio
Summoning Charm Accio
No description
Confundus Charm Confundo
Causes the victim to become confused and befuddled.
class A(object):
def __init__(self):
self.a = 1
def x(self):
print("A.x")
def y(self):
print("A.y")
def z(self):
print("A.z") class B(A):
def __init__(self):
A.__init__(self)
self.a = 2
self.b = 3
def y(self):
print("B.y")
def z(self):
print("B.z") class C(object):
def __init__(self):
self.a = 4
self.c = 5
def y(self):
print("C.y")
def z(self):
print("C.z") class D(C, B):
def __init__(self):
C.__init__(self)
B.__init__(self)
self.d = 6
def z(self):
print("D.z")
obj = D()
print(obj.a)
print(obj.b)
print(obj.c)
print(obj.d)
obj.x()
obj.y()
obj.z()
输出:
2
3
5
6
A.x
C.y
D.z
python-继承,父类,子类的更多相关文章
- python 继承中的super
python继承中子类访问父类的方法(包括__init__)主要有两种方法,一种是调用父类的未绑定方法,另一种是使用super(仅仅对于新式类),看下面的两个例子: #coding:utf-8 cla ...
- python的父类和子类中关于继承的不同版本的写法
Python 2.7中的继承 在Python 2.7中,继承语法稍有不同,ElectricCar 类的定义类似于下面这样: class Car(object): def __init__(self, ...
- python 子类继承父类__init__(转载)
转载: http://www.jb51.net/article/100195.htm 前言 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__方法 ...
- python之子类继承父类时进行初始化的一些问题
直接看代码: class Person: def __init__(self): self.name = "jack" class Student(Person): def __i ...
- python - class类 (五) 继承补充-子类继承父类属性/函数方法
子类继承父类属性/函数方法: #方式一:(原生方式,不建议使用) class Dongwu(object): def __init__(self,name,sex,old): self.name = ...
- python子类如何继承父类的实例变量?
类型1:父类和子类的实例变量均不需要传递 class A(object): def __init__(self): self.name = "cui" def get_name(s ...
- Python如何在子类里扩展父类的property?
<python cookbook>8.8节讨论子类扩展property时,一开始都晕了,思考了半天才勉强弄懂一点,赶快记下来.废话不多说,先上代码: class Person: def _ ...
- javascript中子类如何继承父类
参考阮一峰的文章:http://javascript.ruanyifeng.com/oop/inheritance.html#toc4 function Shape() { this.x = 0; t ...
- C#中通过类来继承两个接口,父类实例化接口中的方法,子类继承父类,调用方法
实现了父类继承接口,父类实例化接口的方法,子类继承父类,子类调用父类的方法直接使用 代码如下: using System; using System.Collections.Generic; usin ...
- JAVA中子类会不会继承父类的构造方法
声明:刚刚接触java不久,如果理解有错误或偏差望各位大佬强势批判 java中子类能继承父类的构造方法吗? 父类代码: class Father { String name ; //就不set/get ...
随机推荐
- Cloudstack动态修改CPU、内存
环境: CentOS6.4+Cloudstack4.2+xenserver 6.2 动态修改Cloudstack guest 内存.CPU 准备工作: 1.安装 xenserver tools工具包 ...
- Python代码注释
1.单行注释使用# # Code 2.多行注释,成对使用'''或""",三个单撇号或三个双引号 “”” Code “”” 3.多行快捷注释 1).增加注释 选中待注释的多 ...
- 面试题:servlet jsp cook session 背1
一.Servlet是什么?JSP是什么?它们的联系与区别是什么? Servlet是Java编写的运行在Servlet容器的服务端程序,狭义的Servlet是指Servlet接口,广义的Servlet是 ...
- c# 判断网络地址是否存在
方法一:网络地址存在,有可能可以访问,也有可能不能访问.此方法用来判断地址存在. static bool UrlIsExist(String url) { System.Uri u = null; t ...
- if else的执行流程
int main(void) { int a, b; char op; float ans; scanf_s("%d%c%d",&a,&op,1,&b); ...
- sklearn scoring . xgboost.train . ---> rsme
http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter 3.3.1. The scoring pa ...
- CF 1091E New Year and the Factorisation Collaboration
昨晚Good Bye 2018D题没做出来,车翻大了…… 官方题解 传送门 初赛知识:一个无向图所有顶点度数之和为偶数.然而这东西还有一个高端的名字:Handshaking lemma 但是 ...
- Oracle——创建和管理表
一.常见的数据库对象 对象 描述 表 基本的数据存储集合,由行和列组成 视图 从表中抽出的逻辑上相关的数据集合 序列 提供有规律的数值 索引 提高查询的效率 同以词 给对象起别名 二.Oracle 数 ...
- Java IO RandomAccessFile 任意位置读/写
随机读写类 RandomAccessFile的唯一父类是Object,与其他流父类不同.是用来访问那些保存数据记录的文件的,这样你就可以用seek( )方法来访问记录,并进行读写了.这些记录的大小不必 ...
- jquery操作select(取值,设置选中) 基础
每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...