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 ...
随机推荐
- strstr and strpos
啥也不说 直接上代码: <?php $email = 'name@example.com'; $domain = strstr($email, '@'); echo $domain; // 打 ...
- JavaScript(JS)实现省市联动选择下拉列表
在开发一个应用的时候需要用刀省市联动的下拉列表,网上找到不少.但是要么太复杂,难以修改:要么根本就用不了,最后自己在一个示例中提取出数据,然后自己写了一个,简单易懂,适合新手... 代码如下: Pro ...
- Visual Studio + C# + Xamarin = iOS/Android/Windows Apps
Visual Studio 跨平台開發實戰 (1) -- Hello Xamarin! 前言 應用程式發展的腳步,從來沒有停過.從早期的 Windows 應用程式, 到網路時代的 web 應用程式,再 ...
- 【转】http 缓存
原文地址:https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-c ...
- Docker学习笔记_安装和使用nginx
一.软件环境 1.宿主机OS:Win10 64位 2.虚拟机OS:Ubuntu 18.04,虚拟机IP:192.168.8.25 3.Docker安装在虚拟机Ubuntu 18.04上 二.安装过程 ...
- _GNU_SOURCE宏
打开_GNU_SOURCE这个宏可以打开一些功能,比如为了在Linux系统上编译使用带有检测文件type的宏(S_ISxxxx): S_ISREG() //传入stat结构的st_mode,下同.是否 ...
- Jlabel实现内容自动换行
Jlabel实现内容自动换行 摘自:https://blog.csdn.net/zhhtao89/article/details/50179695 2015年12月04日 21:09:27 阅读数 ...
- psimpl_v7_win32_demo
psimpl - generic n-dimensional polyline simplification 通用N维折线简化程序 Author - Elmar de Koning 作者 - Elma ...
- HttpClient connectionTimeout
转自:http://www.cnblogs.com/carlosk/archive/2013/03/12/2956502.html 前几天服务器端的产品经理跑来问我是否有做请求超时和响应超时的处理.我 ...
- [转]windows7远程桌面连接失败:发生身份验证错误。要求的函数不受支持
转至:https://jingyan.baidu.com/article/d169e18604ca86436611d821.html 系统升级后出现远程连接报错,“发生身份验证错误.要求的函数不受支持 ...