zoukankan      html  css  js  c++  java
  • 接口类和抽象类

    https://www.cnblogs.com/Eva-J/articles/7293890.html

    1. java
    # java里的所有类的继承都是单继承,所以抽象类完美的解决了单继承需求中的规范问题
    # 但对于多继承的需求,由于java本身语法的不支持,所以创建了接口Interface这个概念来解决多继承的规范问题
    
    
    1. python
    # python中没有接口类  :
      #  python中自带多继承 所以我们直接用class来实现了接口类
    # python中支持抽象类  : 一般情况下 单继承  不能实例化
      #  且可以实现python代码
    
    
    
    
    # 抽象类
    import abc #利用abc模块实现抽象类
    
    class All_file(metaclass=abc.ABCMeta):
        all_type='file'
        @abc.abstractmethod #定义抽象方法,无需实现功能
        def read(self):
            '子类必须定义读功能'
            with open('filaname') as f:
                pass
    
        @abc.abstractmethod #定义抽象方法,无需实现功能
        def write(self):
            '子类必须定义写功能'
            pass
    
    class Txt(All_file): #子类继承抽象类,但是必须定义read和write方法
        def read(self):
            print('文本数据的读取方法')
        def write(self):
            print('文本数据的读取方法')
    
    class Sata(All_file): #子类继承抽象类,但是必须定义read和write方法
        def read(self):
            print('硬盘数据的读取方法')
    
        def write(self):
            print('硬盘数据的读取方法')
    
    class Process(All_file): #子类继承抽象类,但是必须定义read和write方法
        def read(self):
            print('进程数据的读取方法')
    
        def write(self):
            print('进程数据的读取方法')
    
    wenbenwenjian=Txt()
    
    yingpanwenjian=Sata()
    
    jinchengwenjian=Process()
    
    #这样大家都是被归一化了,也就是一切皆文件的思想
    wenbenwenjian.read()
    yingpanwenjian.write()
    jinchengwenjian.read()
    
    print(wenbenwenjian.all_type)
    print(yingpanwenjian.all_type)
    print(jinchengwenjian.all_type)
    
    
    
    
    # 接口类
    from abc import abstractmethod,ABCMeta
    class Swim_Animal(metaclass=ABCMeta):
        @abstractmethod
        def swim(self):pass
    
    class Walk_Animal(metaclass=ABCMeta):
        @abstractmethod
        def walk(self):pass
    
    class Fly_Animal(metaclass=ABCMeta):
        @abstractmethod
        def fly(self):pass
    
    class Tiger(Walk_Animal,Swim_Animal):
        def walk(self):
            pass
        def swim(self):
            pass
    class OldYing(Fly_Animal,Walk_Animal):pass
    class Swan(Swim_Animal,Walk_Animal,Fly_Animal):pass
    
    
    1. 源码中
      https://stackoverflow.com/questions/100003/what-are-metaclasses-in-python
      https://www.liaoxuefeng.com/wiki/1016959663602400/1017592449371072
    #robot
    # 定义
    # Copied from Jinja2, released under the BSD license.
    # https://github.com/mitsuhiko/jinja2/blob/743598d788528921df825479d64f492ef60bef82/jinja2/_compat.py#L88
    def with_metaclass(meta, *bases):
        """Create a base class with a metaclass."""
        # This requires a bit of explanation: the basic idea is to make a
        # dummy metaclass for one level of class instantiation that replaces
        # itself with the actual metaclass.
        class metaclass(type):
            def __new__(cls, name, this_bases, d):
                return meta(name, bases, d)
        return type.__new__(metaclass, 'temporary_class', (), {})
    
    
    # 使用
    @py2to3
    class ModelObject(with_metaclass(SetterAwareType, object)):
        __slots__ = []
    
        def copy(self, **attributes):
            """Return shallow copy of this object.
    
            :param attributes: Attributes to be set for the returned copy
                automatically. For example, ``test.copy(name='New name')``.
    
            See also :meth:`deepcopy`. The difference between these two is the same
            as with the standard ``copy.copy`` and ``copy.deepcopy`` functions
            that these methods also use internally.
    
            New in Robot Framework 3.0.1.
            """
            copied = copy.copy(self)
            for name in attributes:
                setattr(copied, name, attributes[name])
            return copied
    
    
    
  • 相关阅读:
    Hive中将文件加载到数据库表失败解决办法
    Hive安装及配置
    Hadoop下MapReduce实现Pi值的计算
    CentOS下Hadoop运行环境搭建
    kettle案例实现
    假期周总结报告03
    假期周总结报告02
    假期周进度报告01
    阅读笔记6
    阅读笔记5
  • 原文地址:https://www.cnblogs.com/amize/p/15195841.html
Copyright © 2011-2022 走看看