zoukankan      html  css  js  c++  java
  • python基础--多态

    多态:接口重用,一种接口,多种实现

     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)
  • 相关阅读:
    0523注册审核
    0520三级联动
    0519考试练习题
    0516ajax
    mysql 高级查询
    mysql
    HTML的格局与布局
    css样式表
    HTML表单
    HTML
  • 原文地址:https://www.cnblogs.com/letgo-doo/p/8462843.html
Copyright © 2011-2022 走看看