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 ...
随机推荐
- PHP数组的详细解读
数组的定义 数组的本质是管理和操作一组变量,数组中可以存储任意长度的数据,也可以存储任意类型的数据.数组中的单元称为元素,每个元素包括下标(键)和值,访问元素的时候,是通过下标来访问,包括一维数组,二 ...
- 第十九课 pluginlib&Nodelet
把rgb摄像头的数据转换为laser的时候使用了Nodelet. pluginlib(插件库) 在ros中有一个plugin的包,下面是一个ROS Plugin Registration的例子 上面包 ...
- 编写高质量代码改善C#程序的157个建议——建议51:具有可释放字段的类型或拥有本机资源的类型应该是可释放的
建议51:具有可释放字段的类型或拥有本机资源的类型应该是可释放的 在建议50中,我们将C#中的类型分为:普通类型和继承了IDisposable接口的非普通类型.非普通类型除了包含那些托管资源的类型外, ...
- 【Android学习】Android工程资源命名禁忌
在制作一个继续按钮时,将button的id设置为continue,发现报了错误,error: invalid symbol: 'continue' 一开始还以为是编码问题,后来百度之后才知道安卓And ...
- (一)ASP.NET中JavaScript的中英文(多语言)实现方案
PS: https://github.com/hzlzh/Front-End-Standards/wiki/HTML-CSS-JS-i18n 本文原始思路起源于此网址,请自行查看. 本文只是简单的一个 ...
- Nginx禁止直接通过IP地址访问网站
介绍下在nginx服务器禁止直接通过IP地址访问网站的方法,以避免别人恶意指向自己的IP,有需要的朋友参考下. 有时会遇到很多的恶意IP攻击,在Nginx下可以禁止IP访问. Nginx的默认虚拟主机 ...
- 使用JMeter测试基于WebSocket协议的服务
使用JMeter测试基于WebSocket协议的服务 :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba( ...
- webapi put 请求405问题
put 请求的时候 浏览器会像服务器发送两个请求 如何没做任何配置第一个options请求是会报错的 这是需要配置路由给options作响应 这时options请求就通过了,然后你们会看到你的put ...
- c#读取数据库内容
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- windows service使用log4net 记录日志
最近写了个定时邮件推送的服务,当利用lognet4记录日志时,发现日志并没有记录.后来明白windows 服务一般默认是在C:\Windows\System 或是C:\Windows\System32 ...