跟老齊學(xué)Python之編寫類之四再論繼承
在上一講代碼的基礎(chǔ)上,做進(jìn)一步修改,成為了如下程序,請看官研習(xí)這個程序:
#!/usr/bin/env python
#coding:utf-8
class Person:
def __init__(self, name, email):
self.name = name
self.email = email
class Programmer(Person):
def __init__(self, name,email,lang, system, website):
Person.__init__(self,name,email)
self.lang = lang
self.system = system
self.website = website
class Pythoner(Programmer):
def __init__(self,name,email):
Programmer.__init__(self,name,email,"python","Ubuntu","qiwsir.github.io")
if __name__=="__main__":
writer = Pythoner("qiwsir","qiwsir@gmail.com")
print "name=",writer.name
print "lang=",writer.lang
print "email=",writer.email
print "system=",writer.system
print "website=",writer.website
#運(yùn)行結(jié)果
name= qiwsir
lang= python
email= qiwsir@gmail.com
system= Ubuntu
website= qiwsir.github.io
對結(jié)果很滿意,再看程序中的繼承關(guān)系:Pythoner <-- Programmer <-- Person,從上面的過程中不難看出,繼承能夠減少代碼重復(fù),是的代碼更簡練。另外,在繼承的時候,也可以在函數(shù)中對參數(shù)進(jìn)行默認(rèn)賦值。
為了能夠突出繼承問題的探究,還是用那種簡單的類來做實(shí)驗(yàn)。
多余的B
#!/usr/bin/env python
#coding:utf-8
class A:
def __init__(self):
print "aaa"
class B(A):
pass
if __name__=="__main__":
a = A()
b = B()
#運(yùn)行結(jié)果
aaa
aaa
B繼承A,沒有任何修改地繼承,B就可以不用寫任何東西了,或者說B本質(zhì)上就是一個多余。在真實(shí)的編程過程中,沒有這樣寫的,這里僅僅是為了向看官展示一下繼承的含義罷了。
##首個繼承有效
#!/usr/bin/env python
#coding:utf-8
class A:
def __init__(self):
print "aaa"
class B:
def __init__(self):
print "bbb"
class C1(A,B):
pass
class C2(B,A):
pass
if __name__=="__main__":
print "A--->",
a = A()
print "B--->",
b = B()
print "C1(A,B)--->",
c1 = C1()
print "C2(B,A)--->",
c2 = C2()
#運(yùn)行結(jié)果
A---> aaa
B---> bbb
C1(A,B)---> aaa
C2(B,A)---> bbb
列位看官是否注意了,類C1繼承了兩個類A,B;類C2也繼承了兩個類,只不過書寫順序有點(diǎn)區(qū)別(B,A)。從運(yùn)行結(jié)果可以看出,當(dāng)子類繼承多個父類的時候,對于構(gòu)造函數(shù)__init__(),只有第一個能夠被繼承,第二個就等掉了。所以,一般情況下,不會在程序中做關(guān)于構(gòu)造函數(shù)的同時多個繼承,不過可以接力繼承,就如同前面那個比較真實(shí)的代碼一樣。
其它方法的繼承
#!/usr/bin/env python
#coding:utf-8
class A:
def __init__(self):
print "aaa"
def amethod(self):
print "method a"
class B(A):
def __init__(self):
print "bbb"
if __name__=="__main__":
print "A--->"
a = A()
a.amethod()
print "B--->"
b = B()
b.amethod()
#運(yùn)行結(jié)果
A--->
aaa
method a
B--->
bbb
method a
為了說明白上面的情況,還是畫了一張圖,不過,我畫完之后,就后悔了,看這張圖好像更糊涂了。怎么著也畫了,還是貼出來,如果能夠協(xié)助理解更好了。
A的實(shí)例和調(diào)用,就不多說了。重點(diǎn)看B,類B繼承了A,同時,B在構(gòu)造函數(shù)中自己做了規(guī)定,也就是B的構(gòu)造函數(shù)是按照B的意愿執(zhí)行,不執(zhí)行A的內(nèi)容,但是,A還有一個amethod(self)方法,B則繼承了這個方法。當(dāng)通過類B的實(shí)例調(diào)用這個方法的時候,就能夠成功了:b.amethod()
這就是方法的繼承和調(diào)用方法。
所謂繼承,就是從下到上一級一級地找相應(yīng)的繼承對象,找到了就繼承之。如果有同名的怎么辦?按照什么順序找呢?
應(yīng)用網(wǎng)上的一段:
在Python中,可以進(jìn)行多重繼承,這個時候要注意搜尋的順序,是從子類別開始,接著是同一階層父類別由左至右搜尋,再至更上層同一階層父類別由左至右搜尋,直到達(dá)到頂層為止。
代碼舉例:
class A(object):
def method1(self):
print('A.method1')
def method2(self):
print('A.method2')
class B(A):
def method3(self):
print('B.method3')
class C(A):
def method2(self):
print('C.method2')
def method3(self):
print('C.method3')
class D(B, C):
def method4(self):
print('C.method4')
d = D()
d.method4() # 在 D 找到,C.method4
d.method3() # 以 D->B 順序找到,B.method3
d.method2() # 以 D->B->C 順序找到,C.method2
d.method1() # 以 D->B->C->A 順序找到,A.method1
務(wù)必請真正的學(xué)習(xí)者要對照每個類的每個方法,依次找到相應(yīng)的輸出結(jié)果。從而理解繼承的順序。學(xué)習(xí),就要點(diǎn)滴積累。
- python類和繼承用法實(shí)例
- python類繼承用法實(shí)例分析
- Python中類型關(guān)系和繼承關(guān)系實(shí)例詳解
- Python類的定義、繼承及類對象使用方法簡明教程
- Python類定義和類繼承詳解
- Python中類的定義、繼承及使用對象實(shí)例詳解
- python類繼承與子類實(shí)例初始化用法分析
- python繼承和抽象類的實(shí)現(xiàn)方法
- Python類的多重繼承問題深入分析
- Python中類的繼承代碼實(shí)例
- python類繼承用法實(shí)例分析
- Python實(shí)現(xiàn)類繼承實(shí)例
- 舉例講解Python面向?qū)ο缶幊讨蓄惖睦^承
相關(guān)文章
Python線性擬合實(shí)現(xiàn)函數(shù)與用法示例
這篇文章主要介紹了Python線性擬合實(shí)現(xiàn)函數(shù)與用法,結(jié)合實(shí)例形式分析了Python使用線性擬合算法與不使用線性擬合算法的相關(guān)算法操作技巧,需要的朋友可以參考下2018-12-12Python matplotlib實(shí)現(xiàn)條形統(tǒng)計(jì)圖
這篇文章主要為大家詳細(xì)介紹了Python matplotlib實(shí)現(xiàn)條形統(tǒng)計(jì)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04Python使用裝飾器進(jìn)行django開發(fā)實(shí)例代碼
這篇文章主要介紹了Python使用裝飾器進(jìn)行django開發(fā)實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02pytho多張圖片的無損拼接的實(shí)現(xiàn)示例
很多人都會是用PS進(jìn)行拼接,本文主要介紹了pytho多張圖片的無損拼接的實(shí)現(xiàn)示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07PyQt5?QLineEdit校驗(yàn)器限制輸入實(shí)例代碼
QLineEdit類是一個單行文本控件,可輸入單行字符串,可以設(shè)置回顯模式(Echomode)和掩碼模式,下面這篇文章主要給大家介紹了關(guān)于PyQt5?QLineEdit校驗(yàn)器限制輸入的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05