zoukankan      html  css  js  c++  java
  • 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!
    

      

  • 相关阅读:
    用户反馈
    Alpha版本测试报告
    Alpha Scrum7
    #Alpha Scrum6
    Alpha Scrum5
    #Alpha Scrum4
    Alpha Scrum3
    Alpha Scrum2
    课程总结
    实验九
  • 原文地址:https://www.cnblogs.com/bittorrent/p/4052707.html
Copyright © 2011-2022 走看看