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!
    

      

  • 相关阅读:
    ➡️➡️➡️IELTS reading by Simon on Bili
    lc0502
    lc0331
    lc0329
    lc0327
    lc0326
    lc0324
    lc0320
    lc0319
    lc0316
  • 原文地址:https://www.cnblogs.com/bittorrent/p/4052707.html
Copyright © 2011-2022 走看看