標籤: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
類的特殊方法