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)  ###一个接口 多种状态。
###严格意义上的 多态方法

posted @ 2018-03-22 09:54  滕虎  阅读(104)  评论(0编辑  收藏  举报