多态

#-*-coding:utf-8-*-
__author__ = "logan.xu"

#同一种接口,多种实现
#作用:为了实现接口的重用


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

def talk(self):
pass

@staticmethod
def aniaml_talk(obj):
obj.talk()

class Cat(Animal):
def talk(self):
print("Meow!Meow!")

class Dog(Animal):
def talk(self):
print("Woof!Woof!")

#实例化

d = Dog("it's a dog")
#d.talk()

c = Cat("it's a cat ")
#c.talk()

Animal.aniaml_talk(c)
Animal.aniaml_talk(d)


>>>>>>>>>>>>>>>>>>>>>

Meow!Meow!
Woof!Woof!

posted @ 2018-04-24 17:42  裸奔的小鸵鸟  阅读(129)  评论(0编辑  收藏  举报