1. 程式人生 > >python 類定義 繼承

python 類定義 繼承

src body 默認 father 系統 intel minute 調用 lshell

0 前言

  系統:win7 64bit

  IDE : python(x,y) 2.7.6.1

  IDE集成的解釋器:Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32

1 單繼承

1.1 定義一個時間類,帶默認參數

技術分享圖片
class mytime(object):
    def __init__(self,hour=21,                  minute=13,                  second=50):
        self.hour   = hour
        self.minute = minute
        self.second = second
        
    def __str__(self):
        return ‘__str__ hour:minute:second = %02d:%02d:%02d‘                %(self.hour,self.minute,self.second)
    
    def get_hour(self):
        return self.hour
    
    def get_minute(self):
        return self.minute
    
    def get_second(self):
        return self.second

    def print_time(self):
        print  ‘hour:minute:second = %02d:%02d:%02d‘ %(self.hour,self.minute,self.second)
  
技術分享圖片

說明: 構造函數 __init__(self,hour=21, minute=13, second=50) 帶默認參數的定義方式 ;

    用於輸出的函數 __str__

得到某屬性值的方法 get_xxxx

輸出屬性值的方法 print_time

1.2 定義日期時間類,繼承時間類的屬性和方法

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class mydatetime(mytime): def __init__(self,year =
2016,mounth = 1,day = 1, \ hour=21, minute=13,second=50): super(mydatetime,self).__init__(hour,minute,second) self.year = year self.mounth = mounth self.day = day def __str__(self): return ‘__str__ year--mounth--day = %02d--%02d--%02d‘
\ %(self.year,self.mounth,self.day) def __del__(self): """ __del__ mydatetime destroyed """ print "__del__ mydatetime destroyed" def print_date(self): print ‘year-mounth-day = %04d-%02d-%02d‘ \ %(self.year,self.mounth,self.day) # self.print_time()

說明: mydatetime 繼承 mytime 的屬性值,這裏mydatetime(子類),mytime(父類)

   構造函數 __init__(self,year = 2016,mounth = 1,day = 1, hour=21, minute=13,second=50) 帶默認參數的定義方式 ,註意要通過 super初始化父類屬性值;

   用於輸出的函數 __str__

輸出屬性值的方法 print_date

1.3 測試

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 if __name__ == ‘__main__‘: print "\n" print "--"*10," mytime t0:" t0 = mytime() print t0 t0.print_time() print t0.get_hour() print "\n" print "--"*10," mytime t1:" t1 = mytime(hour=7,second=20) t1.print_time() print "\n" print "--"*10," mydatetime da:" da = mydatetime(minute=40) print da # da.print_date() da.print_time() del da

  輸出:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 >>> runfile(‘F:/Python/hysrc_py/untitled0_class.py‘, wdir=r‘F:/Python/hysrc_py‘) -------------------- mytime t0: __str__ hour:minute:second = 21:13:50 hour:minute:second = 21:13:50 21 -------------------- mytime t1: hour:minute:second = 07:13:20 -------------------- mydatetime da: __str__ year--mounth--day = 2016--01--01 hour:minute:second = 21:40:50 __del__ mydatetime destroyed >>>

2 多重繼承

  2.1 定義父類 classA classX

1 2 3 4 5 6 7 8 9 10 11 12 class classA(object): def __init__(self): print(‘init action in father class A‘) class classX(object): def __init__(self): print(‘init action in father class X‘)

   2.2 SubClassB 繼承 classA

1 2 3 4 5 6 7 class SubClassB(classA): def __init__(self): print(‘init action in subclass B‘) super(SubClassB,self).__init__()

  2.3 SubClassC 繼承 SubClassB

1 2 3 4 5 6 7 class SubClassC(SubClassB): def __init__(self): print(‘init action in subclass C‘) super(SubClassC,self).__init__()

  2.4 測試

  實例化一個SubClassC類

1 2 3 4 5 if __name__ == ‘__main__‘: print "\n" print "--"*10," SubClassC b:" b = SubClassC()

  由輸出可知:多重繼承時,子類會自動調用父類的構造函數

1 2 3 4 -------------------- SubClassC b: init action in subclass C init action in subclass B init action in father class A

3 多重繼承

  3.1 定義兩個基本類 classA 和 classX

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class classA(object): def __init__(self,a=‘classA‘): self.a = a print(‘init action in father class A‘) def print_a(self): print self.a class classX(object): def __init__(self,x=‘classX‘): self.x = x print(‘init action in father class X‘) def print_x(self): print self.x

3.2 SubClassX 類繼承自 classA 和 classX

1 2 3 4 5 6 7 8 9 10 11 12 13 14 class SubClassX(classA,classX): def __init__(self,a=‘SubClassX:a‘,x=‘SubClassX:x‘): print(‘init action in subclass X‘) super(SubClassX,self).__init__() # classX.__init__(self) self.a = a self.x = x def print_SubX(self): self.print_a() self.print_x()

3.3 實例化一個 SubClassX 類

1 2 3 4 5 6 if __name__ == ‘__main__‘: print "\n" print "--"*10," SubClassX x:" sx = SubClassX() sx.print_SubX()

3.4 由輸出可知 : SubClassX 繼承了 classA 和 classX 的方法和屬性,但不知為何只調用了 classA的初始化函數?

1 2 3 4 5 -------------------- SubClassX x: init action in subclass X init action in father class A SubClassX:a SubClassX:x

3.5 分析:其實在 3.4 的輸出可知,SubClassX只調用了classA的初始化,則classX的屬性是不會自動初始化的,如classX的屬性值x,之所以能夠輸出 "SubClassX:x" ,是由於在 3.2的定義中,手動初試化了 屬性值。self.a = a self.x = x

如下例:

1 2 3 4 5 6 7 8 9 10 11 12 13 class SubClassX(classA,classX): def __init__(self,a=‘SubClassX:a‘,x=‘SubClassX:x‘): print(‘init action in subclass X‘) super(SubClassX,self).__init__(a) # classX.__init__(self) # self.x = x def print_SubX(self): self.print_a() self.print_x()

實例化一個 SubClassX 類:

1 2 3 4 5 6 if __name__ == ‘__main__‘: print "\n" print "--"*10," SubClassX x:" sx = SubClassX() sx.print_SubX()

輸出報錯: 由於定義SubClassX類時,使用 super 初始化父類,只會初試化 classA,即第一個類。而classX排在了classA之後,不會自動初始化。所以在實例化類調用方法時,即 sx.print_SubX() 。由於沒有屬性 ‘x‘ 而報錯。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 -------------------- SubClassX x: init action in subclass X init action in father class A SubClassX:a Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile execfile(filename, namespace) File "F:/Python/hysrc_py/untitled0_class.py", line 111, in <module> sx.print_SubX() File "F:/Python/hysrc_py/untitled0_class.py", line 57, in print_SubX self.print_x() File "F:/Python/hysrc_py/untitled0_class.py", line 24, in print_x print self.x AttributeError: ‘SubClassX‘ object has no attribute ‘x‘ >>>

錯誤提示:屬性錯誤:‘SubClassX‘ 對象 沒有屬性 ‘x‘

python 類定義 繼承