Python學習(23):Python物件導向(1)速成

來源:互聯網
上載者:User

標籤:style   strong   mail   eth   define   target   zha   turn   http   

轉自 http://www.cnblogs.com/BeginMan/p/3190776.html

一、Python經典類與新類

經典類:如果沒有直接或間接的子類化一個對象,也就是說如果沒有指定一個父類,或者是如果子類化的基本類沒有父類,那麼就定義了經典類:

class classics:    ‘define a classics Class‘    pass

 

新式類:與經典類相反。object是“所有類之母”,也就是基類,如果你的類沒有繼承任何父類,object將作為預設的父類:

class newClass(object):    ‘define a classics Class‘    pass

 

二、Python的執行個體化

建立一個執行個體的過程叫執行個體化,在其他程式設計語言中一般用關鍵字 new ,但是在Python中沒有這個關鍵字。而是類似函數調用的方式建立執行個體的:

class newClass(object):    passinst = newClass()print inst #<__main__.newClass object at 0x00B71650>

 

三、類最簡單的用法

類最簡單的用法僅僅用作名稱空間,意味著將資料儲存到變數中,使用句點屬性標識對它們按名稱空間分組。

class newClass(object):    passbar = newClass()    #建立執行個體bar.x = 1 #建立執行個體屬性bar.y = 2print bar.x+bar.y

注意:bar.x  ,bar.y  是執行個體對象的屬性不是類的屬性。

 

四、方法

方法就是類的功能

方法在類中定義,只能被執行個體調用。

在類中,方法有一個預設參數self,這個參數代表執行個體本身,當用執行個體調用方法時,由解譯器悄悄傳遞給方法。

class newClass(object):    def method(self):        return self bar = newClass()print bar.method() #<__main__.newClass object at 0x00B71910>

在其他語言中,self一般稱為this,一般方法會用到這個執行個體(self),但是靜態方法和類方法不需要。

 

五、建立類、方法、執行個體對象訪問

class addPerson(object):    def __init__(self,nm,ph):   #定義構造器 self.name = nm #設定name self.phone = ph #設定phone print ‘Created instance for‘,self.name def updatePhone(self,newph): self.phone = newph print ‘Updated phone for‘,self.phone josn = addPerson(‘JSON‘,‘1565208444‘) #建立執行個體對象josnben = addPerson(‘Ben‘,‘15249638892‘) #建立執行個體對象benprint ‘Instance property:[%s][%s]‘ %(ben.name,ben.phone) #訪問執行個體屬性ben.updatePhone(‘110‘)print ben.phone

 

六、子類

建立子類:靠繼承來進行子類化,在不影響父類(基類)的基礎上改進自己的功能。

如:繼承上面的類

class addInfo(addPerson):    def __init__(self,nm,ph,id,em):         #定義自己的構造器 addPerson.__init__(self, nm, ph) #繼承父類構造器(這個很重要) self.empid = id self.email = em def updateEmail(self,newem): self.email = newem print ‘Updated e-mail address for:‘,self.name zhangsan = addInfo(‘ZhangSan‘,‘123456789‘,‘01‘,‘[email protected]‘) #建立addInfo執行個體,輸出Created instance for ZhangSanprint zhangsan #<__main__.addInfo object at 0x00B71BD0>print ‘Instance property:[%s],[%s],[%s],[%s]‘ %(zhangsan.name,zhangsan.phone,zhangsan.empid,zhangsan.email)#輸出:Instance property:[ZhangSan],[123456789],[01],[[email protected]]zhangsan.updatePhone(‘250‘) #Updated phone for 250zhangsan.updateEmail(‘[email protected]‘) #Updated e-mail address for: ZhangSan

注意:

如果需要,每個子類最好定義自己的構造器,不然的話,基類的構造器將會被調用。然而如果子類重寫基類的構造器,那麼基類的構造器就不會被自動調用,必須顯式寫出來才行,如上的:addPerson.__init__

Python學習(23):Python物件導向(1)速成

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.