python-物件導向(四)——類成員的訪問方式匯總

來源:互聯網
上載者:User

標籤:

 

類成員的訪問方式

 

#!/usr/bin/env python# _*_coding:utf-8 _*_class pepole(object):‘‘‘This is  __doc__ information!!!‘‘‘    country = "CHINA"   #靜態欄位 存在類裡    __city = "BeiJing"   #私人靜態欄位    def siyou(self):     #私人欄位在內部設定方法可以訪問        return pepole.__city    def __init__(self,name,age,weight):        self.name = name        self.age = age    #普通欄位,動態欄位,對象欄位  存在對象裡        self.weight = weight        self.__name2 = "FUCK"   #私人普通欄位,通過內部間接訪問,也可通過特殊方法訪問    def __call__(self, *args, **kwargs):        print ‘This is __call__!!!‘    def func(self):         #括弧裡至少一個參數,可以多個        print self.__name2  #內部簡介訪問私人欄位        return  "123"    def __infunc(self):   #私人方法,通過內部簡介訪問        print "This is __infunc!"    def run_infunc(self):  #通過內部間接的訪問私人方法        self.__infunc()    @classmethod     #類方法    def class_func(cls):     #括弧裡只能有一個參數 cls        return  "class_method"    @staticmethod   #靜態方法括弧裡不需要參數(),可以有個多參數    def sts():        print "This is staticmethod"    @staticmethod      #靜態私人方法,屬於類,內部間接訪問    def __static_func():    #靜態方法括弧裡不需要參數(),可以有個多參數        print "This is __static_func"    def run_static(self):  #簡介訪問靜態私人方法        pepole.__static_func()    @property     #屬性把一個方法偽造成一個欄位、一個屬性,像欄位那樣的屬性而不是一個功能    def att(self):    #只能有一個self參數        return  "property"    def __str__(self):        return "This is __str__"#class Son(pepole):  #繼承people的衍生類別,不能拿到父類中的私人方法或欄位#    def show(self):        #print pepole.__city#        passobj = pepole("chenchao",18,70)print obj.__doc__#查看類裡的注釋資訊obj()   #執行__call__ 方法  在對象的後面加()printobj.country    #執行靜態欄位print obj.name    #執列欄位self.name = "chenchao"print obj.siyou()  #內部訪問間接的調用私人欄位  __city = "Beijing"print obj.func()   #執行普通方法    def func(self):print pepole.class_func()   #類方法 調用類方法   @classmethodpepole.sts()     #調用靜態方法    @staticmethodprint obj.att      #執行屬性的方法,不帶括弧 @propertyobj.run_infunc()   #執行私人方法 (間接訪問) def __func_method()obj.run_static()   #執行靜態私人方法(間接訪問)print pepole.__dict__ #擷取類的成員,即:靜態欄位,方法print obj.__dict__  #擷取obj對象的成員  普通欄位,私人普通欄位print obj   #類中定義了__str__方法,列印對象預設輸出方法的傳回值print str(obj)   #自動去類中找__str__方法並得到傳回值from test import pepoleobj2 = pepole("zhangsan",22,66)print  obj2.__module__   #表示當前操作的對象在那個模組print obj2.__class__     #表示當前操作的對象的類是什麼print obj._pepole__name2  #特殊方法訪問對象中的私人普通欄位print pepole._pepole__city #特殊方法訪問類中的私人靜態欄位obj._pepole__infunc()  #特殊方法訪問類中的私人方法#obj2 = Son()#obj2.show()   #如果是繼承的關係,那麼衍生類別不能訪問父類中的私人方法或私人欄位  

  

 

isinstance()  issubclass()
#!/usr/bin/env python# _*_coding:utf-8 _*_a = 10class A():    print  "This is class A"class B(A):    print "This is class B"w = B()print isinstance(w,B)       #True    判斷對象w 是否是類B的對象print isinstance(w,A)       #True    類的基類也可行print isinstance(a,int)     #Trueprint issubclass(B,A)       #True    判斷B是A的衍生類別或 A是B的基類print isinstance(B,int)     #False

 

isinstance(obj=對象, cls=類)            檢查obj是否是類 cls 的對象       is instance :執行個體
   issubclass(sub=子類, super=父類) 檢查sub類是否是 super 類的衍生類別    is subclass: 子類

 

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.