• Python_抽象类和接口类


    前言:类是什么?类是从一堆对象中抽取出来的相同的属性和方法的集合,换句话说类也是object。

    抽象类

       概念:  从一堆类中抽取出来的相同的方法的集合,规定了兼容接口

        特点:  1. 只能被继承,不能实例化

                  2. 子类必须继承抽象类中定义的对象方法、绑定类方法、类方法、静态方法

                  3. 可以添加成员属性

                  4. 抽象类中可以定义抽象方法,也可定义正常方法

    # @Time     : 2022/2/22 23:48
    # @Author   : Bella
    # -*- coding: utf-8 -*-
    import abc
    
    
    class Animals(metaclass=abc.ABCMeta):  # metaclass是创建类工厂,一般正常的类是type()创建
        begin = "这是一个抽象类"
    
        @abc.abstractmethod
        def speak(self):
            pass
    
        @abc.abstractmethod
        def eat():
            pass
    
        @abc.abstractclassmethod
        def wc(cls):
            print('Animal都会上厕所')
    
        @abc.abstractstaticmethod
        def sleep():
            pass
    
    class Cat(Animals):
        def speak(self):
            print('Cat都会说话')
    
        def eat():
            print('Cat都会吃饭')
    
        @classmethod
        def wc(cls):
            print('Cat都会上厕所')
    
        @staticmethod
        def sleep():
            print('Cat都会睡觉')
    
    
    aa = Cat()
    print(aa.begin)
    aa.speak()
    Cat.eat()
    Cat.wc()
    aa.sleep()
    

    ------------------------------------------------------------------------------------------------------------

    接口类

        概念:基于一个接口实现的类,强调函数属性相似性。

    # @Time     : 2022/2/23 21:30
    # @Author   : Bella
    # -*- coding: utf-8 -*-
    
    # 1. 普通方式实现接口类
    """
    如果调用接口类运行时,没有接口类中定义的方法会提示报错
    AttributeError: 'WeChat' object has no attribute 'pay'
    
    """
    
    
    class WeChat(object):
        def pays(self, money):
            print('使用微信付款' + str(money))
    
    
    class ZhiFuBao(object):
        def pay(self, money):
            print('使用支付宝付款' + str(money))
    
    
    def pay(PayWay, money):
        PayWay.pay(money)
    
    
    dd = WeChat()
    pay(dd, 50)
    

    ----------------------------------------------------------------------------------------------------

    抽象类和接口类合并:

    # @Time     : 2022/2/23 21:49
    # @Author   : Bella
    # -*- coding: utf-8 -*-
    import abc
    
    """
    利用抽象类,子类必须继承抽象类中定义的方法的强制性
    使得代码不会出现类中方法名不一样、或者遗漏的低级错误
    """
    class AbsInterface(metaclass=abc.ABCMeta):
        @abc.abstractmethod
        def pay(self, money):
            pass
    
    
    class WeChat(AbsInterface):
        def pay(self, money):
            print('使用微信付款' + str(money))
    
    
    class ZhiFuBao(AbsInterface):
        def pay(self, money):
            print('使用支付宝付款' + str(money))
    
    
    def Interface(PayWay, money):
        PayWay.pay(money)
    
    
    dd = WeChat()
    Interface(dd, 50)
    
  • 相关阅读:
    rest-framework之路由
    rest-framework之频率控制
    mysql 模糊查询 concat()
    解决spring使用动态代理
    MySQL巧用sum,case...when...优化统计查询
    解决maven项目中有小红叉的问题
    google ---gson字符串数组用GSON解析然后用逗号隔开拼接,去掉最后一个逗号
    Elicpse使用技巧-打开选中文件文件夹或者包的当前目录
    powdesigner建表
    遍历map
  • 原文地址:https://www.cnblogs.com/blackpink/p/15925641.html
Copyright © 2020-2023  润新知