文章詳情頁(yè)
Python面向?qū)ο蠖鄳B(tài)實(shí)現(xiàn)原理及代碼實(shí)例
瀏覽:116日期:2022-07-11 09:18:34
多態(tài)
指同一個(gè)方法調(diào)用由于對(duì)象不同可能會(huì)產(chǎn)生不同的行為。
關(guān)于多態(tài)要注意以下幾點(diǎn):
多態(tài)是方法的多態(tài),屬性沒有多態(tài)多態(tài)的存在有兩個(gè)必要條件:繼承、方法重寫
代碼如下
class Man: def eat(self): print(’餓了,準(zhǔn)備吃飯’)class Chinese(Man): def eat(self): print(’中國(guó)人用筷子吃飯’)class English(Man): def eat(self): print(’英國(guó)人用刀叉吃飯’)class India(Man): def eat(self): print(’印度人用右手吃飯’)def manEat(m): if isinstance(m,Man): m.eat() else: print(’不能吃飯’)manEat(Chinese())manEat(India())
結(jié)果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測(cè)可用)3. CSS hack用法案例詳解4. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)8. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法9. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明10. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息
排行榜
