接口继承

接口继承就是(基类)父类定义好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之接口继承的更多相关文章

  1. python定义接口继承类

    zxq547 python定义接口继承类invalid syntax解决办法 1 2 3 4 5 6 7 class s_all(metaclass=abc.ABCMeta):     #python ...

  2. python定义接口继承类invalid syntax解决办法

    class s_all(metaclass=abc.ABCMeta): #python2.7用此方法定义接口继承 # __metaclass__ = abc.ABCMeta @abc.abstract ...

  3. python的类的继承-接口继承-归一化设计

    1.先在子类本身找,如果子类没有,会去父类找 class Dad: '这个是爸爸类' money=10#Dad类的数据属性 def __init__(self,name): print("爸 ...

  4. python之路----继承的抽象类和接口类

    抽象类与接口类 接口类 继承有两种用途: 一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类Interface,接口类中定义了一些接口名(就是函数 ...

  5. python 静态 封装 继承 mro 接口 super

    1.静态属性 静态方法  类方法 #!/usr/bin/python env # encoding: utf-8 # 静态属性 静态方法 class Room: tag = 168 def __ini ...

  6. python面向对象编程 继承 组合 接口和抽象类

    1.类是用来描述某一类的事物,类的对象就是这一类事物中的一个个体.是事物就要有属性,属性分为 1:数据属性:就是变量 2:函数属性:就是函数,在面向对象里通常称为方法 注意:类和对象均用点来访问自己的 ...

  7. python基础之继承派生、组合、接口和抽象类

    类的继承与派生 经典类和新式类 在python3中,所有类默认继承object,但凡是继承了object类的子类,以及该子类的子类,都称为新式类(在python3中所有的类都是新式类) 没有继承obj ...

  8. Python学习第十八课——继承,接口继承等

    1.继承:字面意思 # 继承 : 字面意思 class father: pass class grandfather: pass class children(father): # 单继承 pass ...

  9. [技术学习]js接口继承

    js是面向对象语言,但是js又缺乏了面向对象的诸多特性,比如继承,没有接口继承也没有父类继承,因此有时候需要人工来实现继承. 一.首先看下java中面向对象的继承: //定义类鸟类的飞行动作 inte ...

随机推荐

  1. poj 1274 基础二分最大匹配

    #include<stdio.h> #include<string.h> #define N 300 #define inf 0x3fffffff int mark[N],li ...

  2. 【ACM】nyoj_2_括号配对问题_201308091548

    括号配对问题时间限制:3000 ms  |  内存限制:65535 KB 难度:3描述 现在,有一行括号序列,请你检查这行括号是否配对. 输入 第一行输入一个数N(0<N<=100),表示 ...

  3. mongodb--update高级用法

    配合update使用的函数 $set 修改某列的值 $unset 删除某个列 $rename 重命名某个列 $inc 增长某个列 $setOnInsert 当upsert为true时,并且发生了ins ...

  4. COGS——C610. 数对的个数

    http://cogs.pro/cogs/problem/problem.php?pid=610 Description出题是一件痛苦的事情!题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B P ...

  5. MyBatis在注解上使用动态SQL(@select使用if)

    1.用script标签包围,然后像xml语法一样书写 @Select({"<script>", "SELECT * FROM tbl_order", ...

  6. [Cypress] Find and Test Focused Input with Chrome’s DevTools in Cypress

    In this lesson, we’ll add tests that finds a focused input. We’ll use Chrome’s dev tools from inside ...

  7. elasticsearch插件开发

    检索引擎Elasticsearch支持插件模式.有些时候你可能须要安装一些插件.甚至自己开发插件,这里就提供一个開始ES插件开发演示样例,ES版本号为1.5.2. 一.插件类继承自org.elasti ...

  8. BNU 34990 Justice String 2014 ACM-ICPC Beijing Invitational Programming Contest

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34990 DEBUG了非常久,还是legal的推断函数写错了... 此题做法.枚举Stri ...

  9. 36岁IT老人再次随笔——程序员的门槛其实并不高,但却是一个易学难精的行当——IT的快车很快,我常看到不少人摔落下去,但又有不少身手敏捷的人跳了上来 good

    36岁的我,还在IT里面留恋着技术.我不是什么技术牛人,只是不愿离开.搞硬件的朋友对我说:“我以为你是搞硬件的,没想到你软件方面这么厉害?”,搞软件的朋友对我说:“我以为你只是搞软件的,没想到你硬件方 ...

  10. Java Colections 集合类 —— List、ArrayList、Set(HashSet)

    0. List<T> 是一个接口 该接口定义的高级成员函数有: contains() ⇒ 是否包含: String[] people; List names = Arrays.asList ...