1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:James Tao 4 5 #多態:同一個接口多種實現 6 #多太的做用:接口重用,爲了實現類在繼承和派生的時候,保證使用家譜中任一類的實例 7 #的某一屬性時的正確調用 8 9 class Animal(object): 10 def __init__(self,name): 11 self.name=name 12 13 @staticmethod 14 def animal_talk(obj):# 15 obj.talk() 16 17 class dog(Animal): 18 def talk(self): 19 print('%s is Woof' % self.name) 20 21 class cat(Animal): 22 def talk(self): 23 print('%s is Moew' % self.name) 24 25 dog1=dog('A') 26 cat1=cat('B') 27 dog1.talk()# 28 cat1.talk() 29 30 Animal.animal_talk(dog1)#統一調用(只用一個接口animal_talk)
運行結果:python