Python面向?qū)ο笾^承和多態(tài)用法分析
本文實(shí)例講述了Python面向?qū)ο笾^承和多態(tài)用法。分享給大家供大家參考,具體如下:
Python 類的繼承和多態(tài)
Python 類的繼承
在OOP(Object Oriented Programming)程序設(shè)計(jì)中,當(dāng)我們定義一個(gè)class的時(shí)候,可以從某個(gè)現(xiàn)有的class 繼承,新的class稱為子類(Subclass),而被繼承的class稱為基類、父類或超類(Base class、Super class)。
我們先來定義一個(gè)class Person,表示人,定義屬性變量 name 及 sex (姓名和性別);
定義一個(gè)方法print_title():當(dāng)sex是male時(shí),print man;當(dāng)sex 是female時(shí),print woman。參考如下代碼:
class Person(object): def __init__(self,name,sex): self.name = name self.sex = sex def print_title(self): if self.sex == "male": print("man") elif self.sex == "female": print("woman") class Child(Person): # Child 繼承 Person pass May = Child("May","female") Peter = Person("Peter","male") print(May.name,May.sex,Peter.name,Peter.sex) # 子類繼承父類方法及屬性 May.print_title() Peter.print_title()
而我們編寫 Child 類,完全可以繼承 Person 類(Child 就是 Person);使用 class subclass_name(baseclass_name) 來表示繼承;
繼承有什么好處?最大的好處是子類獲得了父類的全部屬性及功能。如下 Child 類就可以直接使用父類的 print_title() 方法
實(shí)例化Child的時(shí)候,子類繼承了父類的構(gòu)造函數(shù),就需要提供父類Person要求的兩個(gè)屬性變量 name 及 sex:
在繼承關(guān)系中,如果一個(gè)實(shí)例的數(shù)據(jù)類型是某個(gè)子類,那它也可以被看做是父類(May 既是 Child 又是 Person)。但是,反過來就不行(Peter 僅是 Person,而不是Child)。
繼承還可以一級(jí)一級(jí)地繼承下來,就好比從爺爺?shù)桨职?、再到兒子這樣的關(guān)系。而任何類,最終都可以追溯到根類object,這些繼承關(guān)系看上去就像一顆倒著的樹。比如如下的繼承樹:
isinstance() 及 issubclass()
Python 與其他語言不同點(diǎn)在于,當(dāng)我們定義一個(gè) class 的時(shí)候,我們實(shí)際上就定義了一種數(shù)據(jù)類型。我們定義的數(shù)據(jù)類型和Python自帶的數(shù)據(jù)類型,比如str、list、dict沒什么兩樣。
Python 有兩個(gè)判斷繼承的函數(shù):isinstance() 用于檢查實(shí)例類型;issubclass() 用于檢查類繼承。參見下方示例:
class Person(object): pass class Child(Person): # Child 繼承 Person pass May = Child() Peter = Person() print(isinstance(May,Child)) # True print(isinstance(May,Person)) # True print(isinstance(Peter,Child)) # False print(isinstance(Peter,Person)) # True print(issubclass(Child,Person)) # True
Python 類的多態(tài)
在說明多態(tài)是什么之前,我們?cè)?Child 類中重寫 print_title() 方法:若為male,print boy;若為female,print girl
class Person(object): def __init__(self,name,sex): self.name = name self.sex = sex def print_title(self): if self.sex == "male": print("man") elif self.sex == "female": print("woman") class Child(Person): # Child 繼承 Person def print_title(self): if self.sex == "male": print("boy") elif self.sex == "female": print("girl") May = Child("May","female") Peter = Person("Peter","male") print(May.name,May.sex,Peter.name,Peter.sex) May.print_title() Peter.print_title()
當(dāng)子類和父類都存在相同的 print_title()方法時(shí),子類的 print_title() 覆蓋了父類的 print_title(),在代碼運(yùn)行時(shí),會(huì)調(diào)用子類的 print_title()
這樣,我們就獲得了繼承的另一個(gè)好處:多態(tài)。
多態(tài)的好處就是,當(dāng)我們需要傳入更多的子類,例如新增 Teenagers、Grownups 等時(shí),我們只需要繼承 Person 類型就可以了,而print_title()方法既可以直不重寫(即使用Person的),也可以重寫一個(gè)特有的。這就是多態(tài)的意思。調(diào)用方只管調(diào)用,不管細(xì)節(jié),而當(dāng)我們新增一種Person的子類時(shí),只要確保新方法編寫正確,而不用管原來的代碼。這就是著名的“開閉”原則:
- 對(duì)擴(kuò)展開放(Open for extension):允許子類重寫方法函數(shù)
- 對(duì)修改封閉(Closed for modification):不重寫,直接繼承父類方法函數(shù)
子類重寫構(gòu)造函數(shù)
子類可以沒有構(gòu)造函數(shù),表示同父類構(gòu)造一致;子類也可重寫構(gòu)造函數(shù);現(xiàn)在,我們需要在子類 Child 中新增兩個(gè)屬性變量:mother 和 father,我們可以構(gòu)造如下(建議子類調(diào)用父類的構(gòu)造方法,參見后續(xù)代碼):
class Person(object): def __init__(self,name,sex): self.name = name self.sex = sex class Child(Person): # Child 繼承 Person def __init__(self,name,sex,mother,father): self.name = name self.sex = sex self.mother = mother self.father = father May = Child("May","female","April","June") print(May.name,May.sex,May.mother,May.father) Person
若父類構(gòu)造函數(shù)包含很多屬性,子類僅需新增1、2個(gè),會(huì)有不少冗余的代碼,這邊,子類可對(duì)父類的構(gòu)造方法進(jìn)行調(diào)用,參考如下:
class Person(object): def __init__(self,name,sex): self.name = name self.sex = sex class Child(Person): # Child 繼承 Person def __init__(self,name,sex,mother,father): Person.__init__(self,name,sex) # 子類對(duì)父類的構(gòu)造方法的調(diào)用 self.mother = mother self.father = father May = Child("May","female","April","June") print(May.name,May.sex,May.mother,May.father)
多重繼承
多重繼承的概念應(yīng)該比較好理解,比如現(xiàn)在需要新建一個(gè)類 baby 繼承 Child , 可繼承父類及父類上層類的屬性及方法,優(yōu)先使用層類近的方法,代碼參考如下:
class Person(object): def __init__(self,name,sex): self.name = name self.sex = sex def print_title(self): if self.sex == "male": print("man") elif self.sex == "female": print("woman") class Child(Person): pass class Baby(Child): pass May = Baby("May","female") # 繼承上上層父類的屬性 print(May.name,May.sex) May.print_title() # 可使用上上層父類的方法 class Child(Person): def print_title(self): if self.sex == "male": print("boy") elif self.sex == "female": print("girl") class Baby(Child): pass May = Baby("May","female") May.print_title() # 優(yōu)先使用上層類的方法
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python面向?qū)ο笾惖睦^承詳解
- Python面向?qū)ο蠓庋b繼承和多態(tài)示例講解
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)之繼承、多態(tài)原理與用法詳解
- Python面向?qū)ο笾^承原理與用法案例分析
- Python 面向?qū)ο笾庋b、繼承、多態(tài)操作實(shí)例分析
- Python3.5面向?qū)ο蟪绦蛟O(shè)計(jì)之類的繼承和多態(tài)詳解
- Python3.5面向?qū)ο笈c繼承圖文實(shí)例詳解
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)類的封裝與繼承用法示例
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)多繼承和多態(tài)用法示例
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)之類的定義與繼承簡(jiǎn)單示例
- Python面向?qū)ο笾惖亩x與繼承用法示例
- Python面向?qū)ο箢惥帉懠?xì)節(jié)分析【類,方法,繼承,超類,接口等】
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)OOP入門教程【類,實(shí)例,繼承,重載等】
- Python面向?qū)ο笾^承和組合用法實(shí)例分析
- Python面向?qū)ο蟪绦蛟O(shè)計(jì)之繼承與多繼承用法分析
- Python面向?qū)ο箢惖睦^承實(shí)例詳解
- Python面向?qū)ο箢惱^承和組合實(shí)例分析
- Python 面向?qū)ο缶幊痰娜筇匦灾^承
相關(guān)文章
PyCharm運(yùn)行bash腳本的實(shí)現(xiàn)
本文主要介紹了PyCharm運(yùn)行bash腳本的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06pandas中DataFrame排序及分組排序的實(shí)現(xiàn)示例
本文主要介紹了pandas中DataFrame排序及分組排序,pandas中的sort_values()函數(shù)原理類似于SQL中的order by,可以將數(shù)據(jù)集依照某個(gè)字段中的數(shù)據(jù)進(jìn)行排序,下面就來具體介紹一下,感興趣的可以了解一下2024-04-04requests.post()方法中data和json參數(shù)的使用
這篇文章主要介紹了requests.post()方法中data和json參數(shù)的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02