1.靜態方法python
class Dog(object): def __init__(self,name): self.name = name @staticmethod #靜態方法,實際上跟類沒什麼關係了,只是名義上歸類管,實際上在靜態方法裏訪問不了類或實例中任何屬性 def eating(name): print("%s is eating %s"%(name,"baozi")) d = Dog("crh") d.eating("fy")
2.類方法ide
# 類方法 class Dog(object): name = "cqk" def __init__(self, name): self.name = name @classmethod #類方法,只能訪問類變量,不能訪問實例變量 def eating(self): print("%s is eating %s" % (self.name, "baozi")) d = Dog("fy") d.eating()
3.屬性方法函數
class Dog(object): def __init__(self, name): self.name = name @property #將一個方法變成一個靜態屬性 def eating(self): print("%s is eating %s" % (self.name, "baozi")) @eating.setter #加上這個裝飾函數以後就能夠給屬性方法賦值 def eating(self,food): print("set the food:%s"%(food)) @eating.deleter #加上這個裝飾函數以後就能夠刪掉該屬性方法 def eating(self): print("刪完了") d = Dog("fy") d.eating #調用時就像在調用屬性,不在加括號 d.eating = "tudou" del d.eating