面向对象-多态的实现
python很多语法支持多态,多态的实现方法,实现接口重用
class Animal:
def __init__(self,name):
self.name=name
def talk(self):
pass
class Cat(Animal):
def talk(self):
print("%s Meow..."%self.name)
class Dog(Animal):
def talk(self):
print("%s Woof! woof!"%self.name)
d = Dog("chenronghua")
#d.talk()
c=Cat("xuliangwei")
#c.talk()
def animal_talk(obj):
obj.talk()
animal_talk(c)
animal_talk(d)
-------------------------------------
class Animal:
def __init__(self,name):
self.name=name
def talk(self):
pass
@staticmethod #装饰器
def animal_talk(obj):
obj.talk()
class Cat(Animal):
def talk(self):
print("%s Meow..."%self.name)
class Dog(Animal):
def talk(self):
print("%s Woof! woof!"%self.name)
d = Dog("chenronghua")
#d.talk()
c=Cat("xuliangwei")
#c.talk()
Animal.animal_talk(c)
Animal.animal_talk(d)