類的特殊方法

來源:互聯網
上載者:User

標籤:def   print   @class   執行個體變數   init   eating   tin   執行個體   foo   

```## 靜態方法(只是名義上歸類管理,但實際上在今天方法裡無法訪問類或執行個體中的任何屬性)class cat(object): def __init__(self,name): self.name=name @staticmethod # 實際上和類沒關係了 def eat(self): print("%s is eating %s "%(self.name,"food"))c=cat("alex")c.eat(c) #強行有關係要把執行個體傳進去(其實就是一個函數)# 類方法class cat(object): name="alex" # 類變數 def __init__(self,name): self.name=name @classmethod # 類方法只能訪問類變數,不能訪問執行個體變數 def eat(self): print("%s is eating %s "%(self.name,"food"))c=cat("拉大")c.eat() #強行有關係要把執行個體傳進去(其實就是一個函數)# 屬性方法(把一個方法變成靜態屬性)class cat(object): name="alex" # 類變數 def __init__(self,name): self.name=name self.__food=None @property # 把一個方法變成靜態屬性 def eat(self): print("%s is eating %s " % (self.name,self.__food)) @eat.setter #(修改) def eat(self,food): print("set food is " , food) self.__food=food @eat.deleter #(刪除) def eat(self): del self.__food print("刪完了")c=cat("拉大")c.eatc.eat="漢堡"c.eatdel c.eatc.eat

類的特殊方法

聯繫我們

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