多态:接口重用,一种接口,多种实现
1 __author__ = "Alex Li" 2 3 4 class Animal: 5 def __init__(self, name): # Constructor of the class 6 self.name = name 7 8 def talk(self): # Abstract method, defined by convention only 9 pass #raise NotImplementedError("Subclass must implement abstract method") 10 11 @staticmethod 12 def animal_talk(obj): 13 obj.talk() 14 15 class Cat(Animal): 16 def talk(self): 17 print('Meow!') 18 19 20 class Dog(Animal): 21 def talk(self): 22 print('Woof! Woof!') 23 24 25 d = Dog("陈荣华") 26 #d.talk() 27 28 c = Cat("徐良伟") 29 #c.talk() 30 # 31 # def animal_talk(obj): 32 # obj.talk() 33 34 Animal.animal_talk(c) 35 Animal.animal_talk(d)