Python中的面向?qū)ο缶幊淘斀?上)
創(chuàng)建類
Python 類使用 class 關(guān)鍵字來(lái)創(chuàng)建。簡(jiǎn)單的類的聲明可以是關(guān)鍵字后緊跟類名:
class ClassName(bases):
'class documentation string' #'類文檔字符串'
class_suite #類體
實(shí)例化
通過(guò)類名后跟一對(duì)圓括號(hào)實(shí)例化一個(gè)類
mc = MyClass() # instantiate class 初始化類
‘int()'構(gòu)造器
def __int__(self):
pass
注意:self類似Java的this關(guān)鍵字作用,它代碼指向自身實(shí)例的引用
類屬性
python的屬性與Java和C++等面向?qū)ο笳Z(yǔ)言不同,python的屬性即包括了數(shù)據(jù)成員還包括函數(shù)元素,通過(guò)句點(diǎn)符號(hào)來(lái)訪問(wèn).
特殊數(shù)據(jù)內(nèi)建屬性
C.name 類C的名字(字符串)
C.doc 類C的文檔字符串
C.bases 類C的所有父類構(gòu)成的元組
C.dict 類C的屬性
C.module 類C定義所在的模塊(1.5 版本新增)
C.class 實(shí)例C對(duì)應(yīng)的類(僅新式類中)
特殊方法內(nèi)建屬性
dir():獲得類屬性或者實(shí)例屬性名字列表.
靜態(tài)變量屬性
直接在class作用域定義
class C(object):
foo = 100
實(shí)例變量屬性
python的實(shí)例屬性與Java和C++等不同.在Java和C++中,實(shí)例屬性必須首先聲明/定義,而python實(shí)例屬性是動(dòng)態(tài)創(chuàng)建。設(shè)置實(shí)例的屬性可以在實(shí)例創(chuàng)建后任意時(shí)間進(jìn)行,也可以在能夠訪問(wèn)實(shí)例的代碼中進(jìn)行。構(gòu)造
器init()是設(shè)置這些屬性的關(guān)鍵點(diǎn)之一。
def __init__(self, name, data):
self.name = name
self.data = "123'
注意:self類似Java的this關(guān)鍵字作用,它代碼指向自身實(shí)例的引用
方法屬性
分為實(shí)例方法和類方法.實(shí)例方法只屬于一個(gè)實(shí)例;而類方法即屬于類所有,也屬于實(shí)例所有.
實(shí)例方法
class MyClass(object):
def myNoActionMethod(self):
pass
注意:self類似Java的this關(guān)鍵字作用,它代碼指向自身實(shí)例的引用
靜態(tài)方法
靜態(tài)方法是類級(jí)別的方法,不需要實(shí)例化類就可以直接調(diào)用.有兩種方法定義
●裝飾器(常用)
@staticmethod
def foo():
print 'call static method'
●內(nèi)建函數(shù)
def foo():
print 'call static method'
foo = staticmethod(foo) #靜態(tài)方法
類方法
靜態(tài)方法是類級(jí)別的方法, 與靜態(tài)方法不同的是,它必須顯示傳入cls類參數(shù);而且如果還需要調(diào)用類中其他的靜態(tài)方法,或者類方法的函數(shù), 要定義成類方法. 與靜態(tài)方法類似,也有兩種方法定義.
●裝飾器(常用)
@classmethod
def bar(cls):
print 'call class method and access static varible(staticVar): ', cls.staticVar
●內(nèi)建函數(shù)
def bar(cls):
print 'call class method and access static varible(staticVar): ', cls.staticVar
bar = classmethod(bar) #類方法
實(shí)例詳解
#!/usr/bin/python
#coding=utf-8
class Target(): #定義類Target
'This is Target definition' #定義__doc__屬性
staticVar = 'v1.0' #定義靜態(tài)變量
def __init__(self, name = 'default', data = 0): #定義構(gòu)造函數(shù)
self.name = name #實(shí)例變量
self.data = data #實(shí)例變量
print "init instance"
def main():
print "this is a test function"
'''
可以用裝飾器定義靜態(tài)方法
@staticmethod
def foo():
print 'call static method'
'''
def foo():
print 'call static method'
foo = staticmethod(foo) #靜態(tài)方法
'''
可以用裝飾器定義類方法
@classmethod
def bar(cls):
print 'call class method and access static varible(staticVar): ', cls.staticVar
'''
def bar(cls):
print 'call class method and access static varible(staticVar): ', cls.staticVar
bar = classmethod(bar) #類方法
#只有調(diào)用本模塊的時(shí)候main()方法才生效
if __name__ == '__main__':
main()
#實(shí)例化
target = Target('aaa', 123)
print 'name is: ', target.name
print 'data is: ', target.data
#打印__doc__屬性
print 'target.__doc__ is: ', target.__doc__
#打印類__dict__屬性
print 'Target.__dict__ is: ', Target.__dict__
#打印靜態(tài)變量
print 'staticVar is: ', Target.staticVar
#打印內(nèi)建函數(shù)dir()
print 'dir() is: ', dir(Target)
#調(diào)用靜態(tài)方法
Target.foo()
#調(diào)用類方法
Target.bar()
輸出
this is a test function
init instance
name is: aaa
data is: 123
target.__doc__ is: This is Target definition
Target.__dict__ is: {'__module__': '__main__', 'foo': <staticmethod object at 0x7f3fd9310cc8>, 'bar': <classmethod object at 0x7f3fd9310d38>, 'staticVar': 'v1.0', 'main': <function main at 0x7f3fd930e758>, '__doc__': 'This is Target definition', '__init__': <function __init__ at 0x7f3fd930e6e0>}
staticVar is: v1.0
dir() is: ['__doc__', '__init__', '__module__', 'bar', 'foo', 'main', 'staticVar']
call static method
call class method and access static varible(staticVar): v1.0
- Python入門篇之面向?qū)ο?/a>
- Python面向?qū)ο笾蓄悾╟lass)的簡(jiǎn)單理解與用法分析
- Python面向?qū)ο笾惡蛯?duì)象實(shí)例詳解
- Python 使用 attrs 和 cattrs 實(shí)現(xiàn)面向?qū)ο缶幊痰膶?shí)踐
- Python 面向?qū)ο?成員的訪問(wèn)約束
- Python面向?qū)ο髮?shí)現(xiàn)一個(gè)對(duì)象調(diào)用另一個(gè)對(duì)象操作示例
- Python3.5面向?qū)ο缶幊虉D文與實(shí)例詳解
- Python面向?qū)ο箢惖睦^承實(shí)例詳解
- Python?面向?qū)ο缶幊淘斀?/a>
相關(guān)文章
把MySQL表結(jié)構(gòu)映射為Python中的對(duì)象的教程
這篇文章主要介紹了簡(jiǎn)單地把MySQL表結(jié)構(gòu)映射為Python中的對(duì)象的方法,用到了Python中的SQLAlchemy庫(kù),需要的朋友可以參考下2015-04-04Python實(shí)現(xiàn)的檢測(cè)web服務(wù)器健康狀況的小程序
這篇文章主要介紹了Python實(shí)現(xiàn)的檢測(cè)web服務(wù)器健康狀況的小程序,本文使用socket庫(kù)來(lái)實(shí)現(xiàn),需要的朋友可以參考下2014-09-09舉例講解Linux系統(tǒng)下Python調(diào)用系統(tǒng)Shell的方法
這篇文章主要介紹了舉例講解Linux系統(tǒng)下Python調(diào)用系統(tǒng)Shell的方法,包括用Python和shell讀取文件某一行的實(shí)例,需要的朋友可以參考下2015-11-11Python使用Turtle模塊繪制國(guó)旗的方法示例
這篇文章主要給大家介紹了關(guān)于Python使用Turtle模塊繪制國(guó)旗的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02python使用tkinter模塊實(shí)現(xiàn)文件選擇功能
這篇文章主要介紹了python使用tkinter模塊實(shí)現(xiàn)文件選擇功能,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06運(yùn)用PyTorch動(dòng)手搭建一個(gè)共享單車預(yù)測(cè)器
這篇文章主要介紹了運(yùn)用PyTorch動(dòng)手搭建一個(gè)共享單車預(yù)測(cè)器,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08django restframework序列化字段校驗(yàn)規(guī)則
本文主要介紹了django restframework序列化字段校驗(yàn)規(guī)則,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05