<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                在上一講代碼的基礎上,做進一步修改,成為了如下程序,請看官研習這個程序: ~~~ #!/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 #運行結果 name= qiwsir lang= python email= qiwsir@gmail.com system= Ubuntu website= qiwsir.github.io ~~~ 對結果很滿意,再看程序中的繼承關系:Pythoner <-- Programmer <-- Person,從上面的過程中不難看出,繼承能夠減少代碼重復,是的代碼更簡練。另外,在繼承的時候,也可以在函數中對參數進行默認賦值。 為了能夠突出繼承問題的探究,還是用那種簡單的類來做實驗。 ## 多余的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() #運行結果 aaa aaa ~~~ B繼承A,沒有任何修改地繼承,B就可以不用寫任何東西了,或者說B本質上就是一個多余。在真實的編程過程中,沒有這樣寫的,這里僅僅是為了向看官展示一下繼承的含義罷了。 ~~~ ##首個繼承有效 #!/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() #運行結果 A---> aaa B---> bbb C1(A,B)---> aaa C2(B,A)---> bbb ~~~ 列位看官是否注意了,類C1繼承了兩個類A,B;類C2也繼承了兩個類,只不過書寫順序有點區別(B,A)。從運行結果可以看出,當子類繼承多個父類的時候,對于構造函數`__init__()`,只有第一個能夠被繼承,第二個就等掉了。所以,一般情況下,不會在程序中做關于構造函數的同時多個繼承,不過可以接力繼承,就如同前面那個比較真實的代碼一樣。 ## 其它方法的繼承 ~~~ #!/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() #運行結果 A---> aaa method a B---> bbb method a ~~~ 為了說明白上面的情況,還是畫了一張圖,不過,我畫完之后,就后悔了,看這張圖好像更糊涂了。怎么著也畫了,還是貼出來,如果能夠協助理解更好了。 [![](https://box.kancloud.cn/2015-07-07_559b3be8102af.png)](https://github.com/qiwsir/ITArticles/blob/master/Pictures/22001.png) A的實例和調用,就不多說了。重點看B,類B繼承了A,同時,B在構造函數中自己做了規定,也就是B的構造函數是按照B的意愿執行,不執行A的內容,但是,A還有一個amethod(self)方法,B則繼承了這個方法。當通過類B的實例調用這個方法的時候,就能夠成功了:b.amethod() 這就是方法的繼承和調用方法。 所謂繼承,就是從下到上一級一級地找相應的繼承對象,找到了就繼承之。如果有同名的怎么辦?按照什么順序找呢? 應用網上的一段: [在Python中,可以進行多重繼承,這個時候要注意搜尋的順序,是從子類別開始,接著是同一階層父類別由左至右搜尋,再至更上層同一階層父類別由左至右搜尋,直到達到頂層為止。](http://openhome.cc/Gossip/Python/Inheritance.html) 代碼舉例: ~~~ 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 ~~~ 務必請真正的學習者要對照每個類的每個方法,依次找到相應的輸出結果。從而理解繼承的順序。學習,就要點滴積累。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看