久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Python中的面向?qū)ο缶幊淘斀?上)

 imelee 2016-12-15

創(chuàng)建類

Python 類使用 class 關鍵字來創(chuàng)建,。簡單的類的聲明可以是關鍵字后緊跟類名:

復制代碼 代碼如下:

class ClassName(bases):
    'class documentation string' #'類文檔字符串'
    class_suite #類體

實例化

通過類名后跟一對圓括號實例化一個類

復制代碼 代碼如下:

mc = MyClass() # instantiate class 初始化類
‘int()'構(gòu)造器

def __int__(self):
    pass


注意:self類似Java的this關鍵字作用,它代碼指向自身實例的引用

類屬性

python的屬性與Java和C++等面向?qū)ο笳Z言不同,python的屬性即包括了數(shù)據(jù)成員還包括函數(shù)元素,通過句點符號來訪問.

特殊數(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 實例C對應的類(僅新式類中)

特殊方法內(nèi)建屬性

dir():獲得類屬性或者實例屬性名字列表.

靜態(tài)變量屬性

直接在class作用域定義

復制代碼 代碼如下:

class C(object):
    foo = 100

實例變量屬性

python的實例屬性與Java和C++等不同.在Java和C++中,實例屬性必須首先聲明/定義,而python實例屬性是動態(tài)創(chuàng)建。設置實例的屬性可以在實例創(chuàng)建后任意時間進行,也可以在能夠訪問實例的代碼中進行。構(gòu)造
器init()是設置這些屬性的關鍵點之一,。

復制代碼 代碼如下:

    def __init__(self, name, data):
        self.name = name
        self.data = "123'

注意:self類似Java的this關鍵字作用,它代碼指向自身實例的引用

方法屬性

分為實例方法和類方法.實例方法只屬于一個實例;而類方法即屬于類所有,也屬于實例所有.

實例方法

復制代碼 代碼如下:

class MyClass(object):
    def myNoActionMethod(self):
    pass

注意:self類似Java的this關鍵字作用,它代碼指向自身實例的引用

靜態(tài)方法

靜態(tài)方法是類級別的方法,不需要實例化類就可以直接調(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)方法是類級別的方法, 與靜態(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)  #類方法

實例詳解
復制代碼 代碼如下:

#!/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    #實例變量
        self.data = data    #實例變量
        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)用本模塊的時候main()方法才生效
    if __name__ == '__main__':
        main()

#實例化
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

    本站是提供個人知識管理的網(wǎng)絡存儲空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導購買等信息,,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多