python物件導向的繼承

來源:互聯網
上載者:User

標籤:python

沒什麼可說的,繼承主要就是繼承父類的一些方法,代碼中很詳細

#!/usr/bin/env python #coding:utf-8class Father(object):#新式類    def __init__(self):        self.name='Liu'        self.FamilyName='Yan'            def Lee(self):        print '我是父類函數Lee'        def Allen(self):        print "我是父類函數Allen"        class Son(Father):    def __init__(self):        #Father.__init__(self)   #經典類執行父類建構函式        super(Son,self).__init__()  #新式類執行父類建構函式        self.name='Feng'            def Aswill(self): #子類新增函數        print 'Son.Bar'        def Lee(self):#重寫父類函數Lee        print '子類重寫了父類函數Lee'    s1=Son()print "繼承了父類的姓"+ s1.FamilyNameprint "重寫了父類的名字",s1.names1.Lee() #子類重寫了父類函數Lees1.Allen() #子類繼承了父類函數Allen

繼承多個類時的順序,經典類繼承是深度優先,是一個BUG, 新式類是廣度優先,應該是用新式類去定義類

新式類

class A(object): #新式類的寫法    def __init__(self):        print 'This is from A'        def test(self):        print 'This is test from A'class B(A):    def __init__(self):        print "This is from B"        class C(A):    def __init__(self):        print "This is from C"        def test(self):        print "This is test from C"                class D(B,C):    def __init__(self):        print 'this is D'        T1=D()T1.test()


經典類

class A():    def __init__(self):        print 'This is from A'        def test(self):        print 'This is test from A'class B(A):    def __init__(self):        print "This is from B"        class C(A):    def __init__(self):        print "This is from C"        def test(self):        print "This is test from C"                class D(B,C):    def __init__(self):        print 'this is D'        T1=D()T1.test()




python物件導向的繼承

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.