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