首页 > 代码库 > Python : Polymorphism

Python : Polymorphism

class Animal:    def __init__(self, name):    # Constructor of the class        self.name = name    def talk(self):              # Abstract method, defined by convention only        raise NotImplementedError("Subclass must implement abstract method")class Cat(Animal):    def talk(self):        return ‘Meow!‘class Dog(Animal):    def talk(self):        return ‘Woof! Woof!‘animals = [Cat(‘Missy‘),           Cat(‘Mr. Mistoffelees‘),           Dog(‘Lassie‘)]for animal in animals:    print animal.name + ‘: ‘ + animal.talk()# prints the following:## Missy: Meow!# Mr. Mistoffelees: Meow!# Lassie: Woof! Woof!

  

Python : Polymorphism