zoukankan      html  css  js  c++  java
  • 【Python】接口定义及实现

    一、无返回值

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    """
    @Time    :
    @Author  :
    @File    :
    @Version :1.0
    @Function:
    """
    
    
    # 定义接口
    class InterfaceRoot(object):
        def get_a(self):
            pass
    
        def get_b(self):
            pass
    
        def get_c(self):
            pass
    
    
    # 接口实现1
    class Impl1InterfaceRoot(InterfaceRoot):
        def get_a(self):
            print("Impl1_a")
    
        def get_b(self):
            print("Impl1_b")
    
    
    # 接口实现2
    class Impl2InterfaceRoot(InterfaceRoot):
        def get_a(self):
            print("Impl2_a")
    
        def get_b(self):
            print("Impl2_b")
    
        def get_c(self):
            print("Impl2_c")
    
    
    if __name__ == '__main__':
        print('Impl1InterfaceRoot 实现')
        Impl1InterfaceRoot().get_a()
        Impl1InterfaceRoot().get_b()
        Impl1InterfaceRoot().get_c()
        print('Impl2InterfaceRoot 实现')
        Impl2InterfaceRoot().get_a()
        Impl2InterfaceRoot().get_b()
        Impl2InterfaceRoot().get_c()

    输出

    Impl1InterfaceRoot 实现
    Impl1_a
    Impl1_b
    Impl2InterfaceRoot 实现
    Impl2_a
    Impl2_b
    Impl2_c

    二、有返回值

     -> 后面表示说明此方法的返回值(注释作用)

     ... 表示返回值可以有0个或多个

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    """
    @Time    :
    @Author  :
    @File    :
    @Version :1.0
    @Function:
    """
    
    
    # 定义接口
    class InterfaceRoot(object):
        def get_a(self) -> int: ...
    
        def get_b(self) -> int: ...
    
        def get_c(self) -> int: ...
    
    
    # 接口实现1
    class Impl1InterfaceRoot(InterfaceRoot):
        def get_a(self):
            print("Impl1_a")
            return '接口实现1 get_a 返回值'
    
        def get_b(self):
            print("Impl1_b")
    
    
    # 接口实现2
    class Impl2InterfaceRoot(InterfaceRoot):
        def get_a(self):
            print("Impl2_a")
    
        def get_b(self):
            print("Impl2_b")
    
        def get_c(self):
            print("Impl2_c")
    
    
    if __name__ == '__main__':
        print('---------------------------------------Impl1InterfaceRoot 实现')
        print(Impl1InterfaceRoot().get_a())
        Impl1InterfaceRoot().get_b()
        Impl1InterfaceRoot().get_c()
        print('---------------------------------------Impl2InterfaceRoot 实现')
        print(Impl2InterfaceRoot().get_a())
        Impl2InterfaceRoot().get_b()
        Impl2InterfaceRoot().get_c()

    输出:

    ---------------------------------------Impl1InterfaceRoot 实现
    Impl1_a
    接口实现1 get_a 返回值
    Impl1_b
    ---------------------------------------Impl2InterfaceRoot 实现
    Impl2_a
    None
    Impl2_b
    Impl2_c

    如果忍耐算是坚强 我选择抵抗 如果妥协算是努力 我选择争取
  • 相关阅读:
    CSS3——过渡
    CSS——优雅降级和渐进增强
    jq1 颜色填充器 和清空指定颜色
    1. 初识node
    javaSE- 01
    鼠标悬浮图片时弹出透明提示图层的jQuery特效
    通过jQuery制作电子时钟表的代码
    9种网页Flash焦点图和jQuery焦点图幻灯片
    7种网页图片切换方式代码
    21种网页在线客服代码实例演示
  • 原文地址:https://www.cnblogs.com/danhuai/p/15512210.html
Copyright © 2011-2022 走看看