python類總結

來源:互聯網
上載者:User

標籤:成員   table   line   int   內容   center   item   repeat-x   return   

python 類的總結
  1. 私人屬性

    class Dog():
    def init(self):
    self.heart=100
    d=Dog()
    d.heart #訪問不到,heart屬於私人
    在變數前加
    兩個底線,變數變為私人屬性,外部不能訪問,內部可以訪問
    def getheart(): return self.heart#提供對外的私人屬性的存取方法
    對私人屬性的強制訪問
    d.Dog
    heart

  2. 公有屬性(所有類共同擁有的屬性)class Dog() type=‘京巴‘ #建立公有屬性d=Dog()d2=Dog()Dog.type #這樣調用,是在所有類的對象的公有變數裡設定d.type#d,d2屬性相同 d2.type#當用對象調用的時候,會在自己對象的記憶體裡建立一個type變數
  3. 解構函式
    d=Dog()del d#刪除對象名到記憶體空間的引用
  4. 繼承
  5. 靜態方法
    名義上歸類管理,實際上在靜態方法裡訪問不了類或執行個體中的任何屬性
    author = ‘Mr.Bool‘
    class Dog(object):
    def init(self,name):
    self.name=name
    @staticmethod
    def eat():
    print("%s like eat 骨頭"%self.name)
    print(‘狗愛吃骨頭‘)
    @staticmethod
    def eat(self):
    print("%s like eat 骨頭"%self.name)
    # print(‘狗愛吃骨頭‘)
    def talk(self):
    self.eat(self)
    print(‘汪汪‘)

    d=Dog("汪汪")
    d.talk()

  6. 類方法@classmethod類方法只能訪問類變數。不能訪問執行個體變數。幾乎很少用
  7. 屬性方法@property
    把一個方法變成一個靜態屬性,不用括弧調用。class Dog(): def init(self): pass @property def eat(self): print(‘狗愛吃骨頭‘) @property def eat(self,name): print(‘狗愛吃%s‘%self.name) @eat.setter def eat(self,foot): print(‘set to foot:%s‘%foot) d=Dog() d.eat 調用屬性方法 d.eat=‘包子‘ 當屬性方法需要傳參數,就要重寫一個方法,方法名與屬性方法名相同 d.Eat=‘包子‘ 大寫也可以 當刪除屬性方法時用 @eat.delete def eat(self): del self.__food print(‘刪完了‘)
  8. 類的描述資訊
    class Dog(): ‘‘‘這個類是描述狗的‘‘‘print(Dog.doc)輸出 這個類是描述狗的
  9. moduleclassmodule輸出 這個類是從哪個模組裡class輸出類
  10. call對象後面加括弧執行calld=Dog()d() 將調用Dog裡的call方法
  11. dict查看對象中的所有成員變數和方法,使用執行個體調用只列印執行個體內的屬性,不包括類屬性
  12. str

    在類裡寫
    def str(self): return "<obj:%s>"%self.named直接列印對象就輸出str裡的內容

  13. getitem,setitem,delitem,把類變成字典
    class Foo(object): def init(self): self.data={} pass def getitem(self,key): print(‘getitem‘,key) return self.data.get(key) pass def setitem(self,key,value): print(‘setitem‘,key) self.data[key]=value pass def delitem(self,key): print(‘delitem‘,key) pass obj=Foo() obj[‘name‘]=‘alex‘#自動觸發執行 getitem print(obj[‘name‘] #自動觸發執行 setitem del obj[‘k1‘] #自動觸發執行delitem
  14. new metaclass類也是對象,type為類的類
    class Dog(): passtype(Dog) 會知道類的類為typenew方法在init建構函式方法前執行new裡面調用init
    def new(cls,args,*kwargs):print(‘Foo --new--‘)return object.new(cls) 當return被注釋時,init不會被調用,這句繼承父類的new方法metaclass原類

    class MyType(): def init(self): print("mytype init") 1 pass def call(self): print("mytype call") 2class Dog(): metaclass=MyType def init(self): print("dog init") 4 pass def new(self): print("dog new") 3
    call建立new new建立init

  15. 反射
    getattr setattr hasattr delattrclass Foo(object):

    def __init__(self):
    self.name = ‘wj‘
    def func(self):
    return ‘func‘

    obj = Foo()

    檢查是否含有成員 ####hasattr(obj, ‘name‘)hasattr(obj, ‘func‘)

    擷取成員 ####getattr(obj, ‘name‘)getattr(obj, ‘func‘)

    設定成員 ####setattr(obj, ‘age‘, 18)setattr(obj, ‘show‘, lambda num: num + 1)

    刪除成員 ####delattr(obj, ‘name‘)delattr(obj, ‘func‘)

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.