码迷,mamicode.com
首页 > 编程语言 > 详细

python基础--多态

时间:2018-02-23 19:04:39      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:sub   rac   author   多态   static   pytho   实现   const   基础   

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

 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)

 

python基础--多态

标签:sub   rac   author   多态   static   pytho   实现   const   基础   

原文地址:https://www.cnblogs.com/letgo-doo/p/8462843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!