面向对象-多态

多态

#多态,它指的是一类事物有多种形态
class Animal:
def voice(self):
pass

class cow(Animal):
def voice(self):
print('咩咩咩???')

class mice(Animal):
def voice(self):
print('吱吱吱')

c = cow()
c.voice()

m = mice()
m.voice()
#或者统一接口
def call(obj):
obj.voice()
call(m)
call(c)

说明:使用多态,让程序更灵活。增加了程序的拓展性

 

posted @ 2018-06-24 22:01  lei-jia-ming  阅读(136)  评论(0)    收藏  举报