• 3-22 多态


    class Animal(object):
    def __init__(self,name):
    self.name =name

    class Cat(Animal):
    def tall(self):
    print('miao miao')
    class Dog(Animal):
    def tall(self):
    print('wang wang')

    def aa(ww):
    ww.tall() ###子类传入 父类Animal

    c1 = Cat('阿三')
    #c1.tall()
    d1 = Dog('阿肆')
    #d1.tall()
    aa(d1) ###一个接口 多种状态。

    不是传统意义上的多态

    class Animal(object):   
        def __init__(self,name):
            self.name =name
    
        @staticmethod    ### static方法
        def aa(ww):
            ww.tall()
    
    class Cat(Animal):
            def tall(self):
                print('miao miao')
    class Dog(Animal):
            def tall(self):
                print('wang wang')
    
    # def aa(ww):
    #     ww.tall()  ###子类传入 父类
    
    c1 = Cat('阿三')
    #c1.tall()
    d1 = Dog('阿肆')
    #d1.tall()
    Animal.aa(c1)  ###一个接口 多种状态。
    ###严格意义上的 多态方法

  • 相关阅读:
    GO 函数的参数
    GO 函数
    GO 指针
    GO 结构体
    GO 接口
    码云git常用命令
    GO Map的初步使用
    GO Slice
    GO 数组
    GO 键盘输入和打印输出
  • 原文地址:https://www.cnblogs.com/th-lyc/p/8621712.html
Copyright © 2020-2023  润新知