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-继承,父类,子类的更多相关文章

  1. python 继承中的super

    python继承中子类访问父类的方法(包括__init__)主要有两种方法,一种是调用父类的未绑定方法,另一种是使用super(仅仅对于新式类),看下面的两个例子: #coding:utf-8 cla ...

  2. python的父类和子类中关于继承的不同版本的写法

    Python 2.7中的继承 在Python 2.7中,继承语法稍有不同,ElectricCar 类的定义类似于下面这样: class Car(object): def __init__(self, ...

  3. python 子类继承父类__init__(转载)

    转载: http://www.jb51.net/article/100195.htm 前言 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__方法 ...

  4. python之子类继承父类时进行初始化的一些问题

    直接看代码: class Person: def __init__(self): self.name = "jack" class Student(Person): def __i ...

  5. python - class类 (五) 继承补充-子类继承父类属性/函数方法

    子类继承父类属性/函数方法: #方式一:(原生方式,不建议使用) class Dongwu(object): def __init__(self,name,sex,old): self.name = ...

  6. python子类如何继承父类的实例变量?

    类型1:父类和子类的实例变量均不需要传递 class A(object): def __init__(self): self.name = "cui" def get_name(s ...

  7. Python如何在子类里扩展父类的property?

    <python cookbook>8.8节讨论子类扩展property时,一开始都晕了,思考了半天才勉强弄懂一点,赶快记下来.废话不多说,先上代码: class Person: def _ ...

  8. javascript中子类如何继承父类

    参考阮一峰的文章:http://javascript.ruanyifeng.com/oop/inheritance.html#toc4 function Shape() { this.x = 0; t ...

  9. C#中通过类来继承两个接口,父类实例化接口中的方法,子类继承父类,调用方法

    实现了父类继承接口,父类实例化接口的方法,子类继承父类,子类调用父类的方法直接使用 代码如下: using System; using System.Collections.Generic; usin ...

  10. JAVA中子类会不会继承父类的构造方法

    声明:刚刚接触java不久,如果理解有错误或偏差望各位大佬强势批判 java中子类能继承父类的构造方法吗? 父类代码: class Father { String name ; //就不set/get ...

随机推荐

  1. AlphaPose ubuntu16 python2安装

    #https://www.tensorflow.org/install/install_linux#ValidateYourInstallation #https://github.com/MVIG- ...

  2. Python编写两个数的加减法游戏

    目标: 1.实现两个数的加减法 2.回答者3次输错计算结果后,输出正确结果,并询问回答者是否继续 1.使用常规函数实现两个数的加减法游戏 代码如下: #!/usr/bin/env python # - ...

  3. SqlServer-geography && Spatial result

    说起geography(地理)这个类型,我感觉好陌生,以前真的没有见过,今天在查询某个Address表的时候,却发现了新大陆——Spatial result(空间的结果). (1)表的结构 (2)查询 ...

  4. OpenCV 2.4.13 installed in Ubuntu 14 and CMakeLists Demo

    1. 配置编译器环境 [compiler] sudo apt-get install build-essential 2. 安装OpenCV的依赖包 [required] -dev pkg-confi ...

  5. adb shell unauthorized问题

    出现unauthorized 一般插上usb后,手机会弹出一个要求你授权debugging的对话框,如果没有的话,就是rsa_key有问题: /adb_keys. User-installed key ...

  6. Web网站的几个QPS

    评价一个网站的"大小",处于视角的不同,有很多种衡量的方法,类似文章数,页面数之类的数据非常明显,也没有什么可以争议的.但对于并发来说,争议非常之多,这里就从一个技术的角度开始,谈 ...

  7. 3.Strings 字符串如何工作?----对缓冲区的理解。

    修改Hello World程序向特定的人问好. #include <iostream> #include <string> int main() { std::string n ...

  8. poj 1611 The Suspects(第一道并查集)

    题意: 有N个学生,编号为0-n-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染, 并且这些人如果还参加了别的社团,他所在的社团照样全部感染,社团个数为m,求感染的人数. 输入: n代表人 ...

  9. Mapper配置文件夹

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-// ...

  10. Socket编程(c语言示例)

    转自:http://blog.csdn.net/dxpqxb/article/details/8166423 前言 Socket可以看成在两个程序进行通讯连接中的一个端点,是连接应用程序和网络驱动程序 ...