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!

  

相关文章:

  • 2021-07-23
  • 2022-01-04
  • 2022-03-01
  • 2021-12-25
  • 2022-12-23
  • 2021-08-21
  • 2021-12-14
  • 2022-01-20
猜你喜欢
  • 2021-09-17
  • 2021-12-12
  • 2021-07-17
  • 2021-08-08
  • 2021-11-20
相关资源
相似解决方案