zoukankan      html  css  js  c++  java
  • Python__接口与归一化设计

    接口与归一化设计:

    1.归一化让使用者无需关心对象的类是什么,只需要知道这些对象都具备某些功能就可以了,这极大地降低了使用者的使用难度。

    2.归一化使得高层的外部使用者可以不加区分的处理所有接口兼容的对象集合。

    继承的两种用途:

    一.继承基类的方法,并且做出自己的改变或者拓展(代码重用):实践中,继承的这种用途意义并不是很大,甚至常常是有害的,因为他使得子类与基类出现强耦合。

    二.声明某个子类兼容与某基类,定义一个接口类(模仿java的interface),接口类中定义了一些接口名(就是函数名)且功能,子类继承接口类,并未实现接口类,并且实现接口类,并且实现接口类中的功能。

    class Interface:#定义接口Interface类来模仿接口的概念,python中压根就没有interface关键字来定义一个接口。
    def read(self): #定接口函数read
    pass

    def write(self): #定义接口函数write
    pass


    class Txt(Interface): #文本,具体实现read和write
    def read(self):
    print('文本数据的读取方法')

    def write(self):
    print('文本数据的写方法')

    class Sata(Interface): #磁盘,具体实现read和write
    def du(self):
    print('硬盘数据的读取方法')

    def write(self):
    print('硬盘数据的写方法')

    class Process(Interface):
    def read(self):
    print('进程数据的读取方法')

    def xie(self):
    print('进程数据的写方法')

    t=Txt()
    s=Sata()
    p=Process()


    t.read()
    s.read()
    p.read()

    import abc
    class Interface(metaclass=abc.ABCMeta):#定义接口Interface类来模仿接口的概念,python中压根就没有interface关键字来定义一个接口。
    all_type='file'
    @abc.abstractmethod
    def read(self): #定接口函数read
    pass

    @abc.abstractmethod
    def write(self): #定义接口函数write
    pass


    class Txt(Interface): #文本,具体实现read和write
    def read(self):
    pass

    def write(self):
    pass
    t=Txt()

    print(t.all_type)

  • 相关阅读:
    Quit Procrastinating! 20 Ways to Energize Out of Your Slump
    [转]会让你人生失败的31种原因
    Control Panel Applets
    MemTest
    The server at www.abstractspoon.com is taking too long to respond.
    拖延者 <<拖延心理学>>
    [转]How to Uninstall Windows Movie Maker
    经典街机
    Causes and Cures for Procrastination
    给页面添加关键词和简介
  • 原文地址:https://www.cnblogs.com/wangmengzhu/p/7399327.html
Copyright © 2011-2022 走看看