接口继承

接口继承就是(基类)父类定义好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. (20)Spring Boot Servlet【从零开始学Spring Boot】

    Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet.Filter.Listener.Interceptor 等等. 当使用Spring-Boot时,嵌 ...

  2. 网上有一种错误的做法是:因为每一个双连通分量内的点low[]值都是相同的,则dfs()时,对于一条边(u,v),只需low[u]=min(low[u],low[v]),这样就不用缩点,最后求度数的时候

  3. HDU 1466

    经典DP,这样的递推确实有点难. 把所有直线分成两组,可以知道 m条直线的交点方案数 =(m-r)条平行线与r条直线交叉的交点数  + r条直线本身的交点方案 亦就是  =(m-r)*r+r条之间本身 ...

  4. [Angular] Angular CDK Intro

    1. Installl latest @angular/cli: sudo npm i -g @angular/cli@next The version I used is:6.0.0-rc.10 2 ...

  5. ASP内置对象—Request、Response 、Server、Application 、ObjectContent (二)

    Response (应答)对象 Request 对象.用于在HTTP请求期间,訪问不论什么client浏览器传递给server的信息,包含通过URL传递的參数信息.使用GET方法或POST方法传递的H ...

  6. 【STL容器学习】-关联容器与map的用法

    STL提供了4个关联容器:set.multiset.map和multimap.这些容器提供了通过keyword高速存储和訪问数据元素的能力.Set和map不同意有反复keyword,而multiset ...

  7. 关于Linux静态库和动态库的分析

    关于Linux静态库和动态库的分析 关于Linux静态库和动态库的分析 1.什么是库 在windows平台和linux平台下都大量存在着库. 本质上来说库是一种可运行代码的二进制形式.能够被操作系统加 ...

  8. Spark MLlib介绍

    Spark MLlib介绍 Spark之所以在机器学习方面具有得天独厚的优势,有以下几点原因: (1)机器学习算法一般都有很多个步骤迭代计算的过程,机器学习的计算需要在多次迭代后获得足够小的误差或者足 ...

  9. 确定比赛名次--hdoj

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  10. 2017-3-13 leetcode 4 11 15

    ji那天居然早起了,惊呆我了,眼睛有点儿疼,一直流泪....继续保持 ========================================================== leetco ...