python之接口继承
接口继承
接口继承就是(基类)父类定义好2个函数属性(接口),所有的子类必须有这2个函数属性,缺一不可,不是说省代码的,是用来做强制性约束的
基类里面的方法不用具体的实现,只是一个规范而已
1.1实现一个一切皆文件的概念
class Disk:
def read(self):
pass
def write(self):
pass
class Cdrom:
def read(self):
pass
def write(self):
pass
class Mem:
def read(self):
pass
def write(self):
pass
1.2可以定义一个基类,对上面代码进行改进
class Allfile:
def read(self):
pass
def write(self):
pass
class Disk(Allfile):
def read(self):
print("disk read")
def write(self):
print("disk write")
class Cdrom(Allfile):
def read(self):
print("cdrom read")
def write(self):
print("cdrom write")
class Mem(Allfile):
def read(self):
print("mem read")
def write(self):
print("mem write")
1.3但是子类也可以不按照你规定的出牌,Mem就是不听话,他不定义write的函数属性,然后就会从父类找。父类里面又是pass
class Allfile:
def read(self):
pass
def write(self):
pass
class Disk(Allfile):
def read(self):
print("disk read")
def write(self):
print("disk write")
class Cdrom(Allfile):
def read(self):
print("cdrom read")
def write(self):
print("cdrom write")
class Mem(Allfile):
def read(self):
print("mem read")
m1=Mem()
m1.read()
m1.write() C:\python35\python3.exe D:/pyproject/day25/接口继承.py mem read
1.4所以python就有一个专门的模块来实现这个强制性的约束子类,模块叫abc
导入模块abc,给父类2个属性加上装饰器之后,如果子类再少属性的话,就直接报错了,这样就强制性的约束了子类必须有父类的2个方法了
import abc
class Allfile(metaclass=abc.ABCMeta):
@abc.abstractstaticmethod
def read(self):
pass
@abc.abstractstaticmethod
def write(self):
pass
class Disk(Allfile):
def read(self):
print("disk read")
def write(self):
print("disk write")
class Cdrom(Allfile):
def read(self):
print("cdrom read")
def write(self):
print("cdrom write")
class Mem(Allfile):
def read(self):
print("mem read")
m1=Mem() TypeError: Can't instantiate abstract class Mem with abstract methods write
1.5当子类Mem也加上write这个方法之后就可以正常运行了
import abc
class Allfile(metaclass=abc.ABCMeta):
@abc.abstractstaticmethod
def read(self):
pass
@abc.abstractstaticmethod
def write(self):
pass
class Disk(Allfile):
def read(self):
print("disk read")
def write(self):
print("disk write")
class Cdrom(Allfile):
def read(self):
print("cdrom read")
def write(self):
print("cdrom write")
class Mem(Allfile):
def read(self):
print("mem read")
def write(self):
print("mem write")
m1=Mem()
m1.read()
m1.write() mem read mem write
python之接口继承的更多相关文章
- python定义接口继承类
zxq547 python定义接口继承类invalid syntax解决办法 1 2 3 4 5 6 7 class s_all(metaclass=abc.ABCMeta): #python ...
- python定义接口继承类invalid syntax解决办法
class s_all(metaclass=abc.ABCMeta): #python2.7用此方法定义接口继承 # __metaclass__ = abc.ABCMeta @abc.abstract ...
- python的类的继承-接口继承-归一化设计
1.先在子类本身找,如果子类没有,会去父类找 class Dad: '这个是爸爸类' money=10#Dad类的数据属性 def __init__(self,name): print("爸 ...
- python之路----继承的抽象类和接口类
抽象类与接口类 接口类 继承有两种用途: 一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类Interface,接口类中定义了一些接口名(就是函数 ...
- python 静态 封装 继承 mro 接口 super
1.静态属性 静态方法 类方法 #!/usr/bin/python env # encoding: utf-8 # 静态属性 静态方法 class Room: tag = 168 def __ini ...
- python面向对象编程 继承 组合 接口和抽象类
1.类是用来描述某一类的事物,类的对象就是这一类事物中的一个个体.是事物就要有属性,属性分为 1:数据属性:就是变量 2:函数属性:就是函数,在面向对象里通常称为方法 注意:类和对象均用点来访问自己的 ...
- python基础之继承派生、组合、接口和抽象类
类的继承与派生 经典类和新式类 在python3中,所有类默认继承object,但凡是继承了object类的子类,以及该子类的子类,都称为新式类(在python3中所有的类都是新式类) 没有继承obj ...
- Python学习第十八课——继承,接口继承等
1.继承:字面意思 # 继承 : 字面意思 class father: pass class grandfather: pass class children(father): # 单继承 pass ...
- [技术学习]js接口继承
js是面向对象语言,但是js又缺乏了面向对象的诸多特性,比如继承,没有接口继承也没有父类继承,因此有时候需要人工来实现继承. 一.首先看下java中面向对象的继承: //定义类鸟类的飞行动作 inte ...
随机推荐
- netty心跳机制和断线重连(四)
心跳是为了保证客户端和服务端的通信可用.因为各种原因客户端和服务端不能及时响应和接收信息.比如网络断开,停电 或者是客户端/服务端 高负载. 所以每隔一段时间 客户端发送心跳包到客户端 服务端做出心 ...
- 洛谷 P1383 codevs 3333 高级打字机
题目描述 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序,支持如下3种操作: 1.T x:在文章末尾打下一个小写字母x.(t ...
- 5.2 calendar--通用日期的相关函数(4)
calendar类提供以下的函数来推断日历相关的内容: calendar.setfirstweekday(weekday) 设置一周里那一天作为第一天.0是表示星期一,6是表示星期天. 样例: #py ...
- POJ 3130
这题,加了精度错了,不加精度反而对了... #include <iostream> #include <cstdio> #include <cstring> #in ...
- keil uV4一个project内各个后缀名文件的作用
1 test1 无后缀文件,这个是终于生成的文件.仅仅要有这个文件KEIL就能够软件仿真,不能打开 2 test1.hex 这个文件能够直接下载到单片机里,他就是从无后缀文件test1里提取的,去掉了 ...
- c21---结构体
// // main.c // 结构体基本概念 // #include <stdio.h> int main(int argc, const char * argv[]) { /* 基本数 ...
- tflearn anaconda 安装过程记录
准备工作:gcc升级为4.8.2glibc升级为2.18 /opt/xxx/xxx/components/ficlient/bigdata_env 里加入:export LD_LIBRARY_PATH ...
- 建模:3D建模
ylbtech-建模:3D建模 “3D建模”通俗来讲就是通过三维制作软件通过虚拟三维空间构建出具有三维数据的模型.3D建模大概可分为:NURBS和多边形网格. NURBS对要求精细.弹性与复杂的模型有 ...
- (Go)01.Windows 安装 Go语言开发环境以及使用
一.Go语言下载 go语言官方下载地址:https://golang.org/dl/ 找到适合你系统的版本下载,本人下载的是windows msi版本.也可以下载Source自己更深层次研究go语言 ...
- JXOI2019 退役记
day0 考前一天在机房和RyeCatcher,还有高一数竞大佬wyt一起颓三国杀,被深深吸引无法自拔,所谓大考大浪,也算是缓解缓解压力 刷刷空间发现好多外地OIer都赶到江科了,萌生出去见一见我江西 ...