文章詳情頁
Python面向?qū)ο蠖鄳B(tài)實現(xiàn)原理及代碼實例
瀏覽:118日期:2022-07-11 09:18:34
多態(tài)
指同一個方法調(diào)用由于對象不同可能會產(chǎn)生不同的行為。
關于多態(tài)要注意以下幾點:
多態(tài)是方法的多態(tài),屬性沒有多態(tài)多態(tài)的存在有兩個必要條件:繼承、方法重寫
代碼如下
class Man: def eat(self): print(’餓了,準備吃飯’)class Chinese(Man): def eat(self): print(’中國人用筷子吃飯’)class English(Man): def eat(self): print(’英國人用刀叉吃飯’)class India(Man): def eat(self): print(’印度人用右手吃飯’)def manEat(m): if isinstance(m,Man): m.eat() else: print(’不能吃飯’)manEat(Chinese())manEat(India())
結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
排行榜
