#靜態(tài)方法用的少,相當(dāng)于類的工具包,訪問不了類或?qū)嵗械娜魏螌傩?br/>classDog(object):
def__init__(self,name):
self.name=name
@staticmethod#實(shí)際上跟類沒關(guān)系了,就只是一個(gè)函數(shù)了,名義上歸類管
defeat(self):
print("%s is eating %s"%(self.name,'dd'))
deftalk(self):
print("%s is talking"%self.name)
d=Dog("chenronghua")
d.eat(d)#把自己傳到eat函數(shù),使self有效
d.talk()
成都創(chuàng)新互聯(lián)擁有一支富有激情的企業(yè)網(wǎng)站制作團(tuán)隊(duì),在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)深耕10余年,專業(yè)且經(jīng)驗(yàn)豐富。10余年網(wǎng)站優(yōu)化營銷經(jīng)驗(yàn),我們已為上1000家中小企業(yè)提供了成都網(wǎng)站建設(shè)、成都網(wǎng)站制作解決方案,按需設(shè)計(jì),設(shè)計(jì)滿意,售后服務(wù)無憂。所有客戶皆提供一年免費(fèi)網(wǎng)站維護(hù)!
#類方法基本用不到
class Dog(object):
name="huazai"
def __init__(self,name):
self.name=name
self.n=2222
@classmethod #類方法只能訪問類變量,不能訪問實(shí)例變量
def eat(self):
print("%s is eating %s"%(self.name,'dd'))
def talk(self):
print("%s is talking"%self.name)
d=Dog("chenronghua")
d.eat()
d.talk()
#屬性方法
class Dog(object):
def__init__(self,name):
self.name=name
@property
defeat(self):
print("%s is eating %s"%(self.name,'dd'))
@eat.setter
defeat(self,food):
print("set to food:",food)
d=Dog("chenronghua")
d.eat # @property下面的eat
d.eat="baozi"# @eat.setter 傳入food
--————————————————————————————————————————
class Dog(object):
def__init__(self,name):
self.name=name
self.__food=None#空變量
@property #方法轉(zhuǎn)屬性(函數(shù)轉(zhuǎn)變量)
defeat(self):
print("%s is eating %s"%(self.name,self.__food))#先傳一個(gè)空變量 self.__food
@eat.setter #修改 修改food值
defeat(self,food): #修改 修改food值
print("set to food:",food)
self.__food=food#將food傳入 空變量
@eat.deleter#刪除
defeat(self):
delself.__food
print("刪完了")
d=Dog("chenronghua")
d.eat # @property下面的eat
d.eat="baozi"# @eat.setter 傳入food
d.eat # "baozi"傳入self.__food,@property下面的 food是"baozi"
deld.eat # @eat,deleter 刪除eat函數(shù) del self.__food刪除self.__food變量
d.eat #依然調(diào)用@property下的eat 提示刪除了Dog.__food變量 即self.__food
# @property
#def eat(self):
# print("%s is eating %s"%(self.name,self.__food))
#屬性方法實(shí)例
classFlight(object):
def__init__(self, name):
self.flight_name = name
defchecking_status(self):
print("checking flight %s status "%self.flight_name)
return2
@property #航班狀態(tài)方法轉(zhuǎn)變成屬性方法
defflight_status(self):
status =self.checking_status()
ifstatus ==0:
print("flight got canceled...")
elifstatus ==1:
print("flight is arrived...")
elifstatus ==2:
print("flight has departured already...")
else:
print("cannot confirm the flight status...,please check later")
@flight_status.setter #修改航班狀態(tài)
defflight_status(self, status):
print("flight %s has changed the flight status to %s"%(self.flight_name,status))
@flight_status.deleter #刪除航班狀態(tài)
defflight_status(self):
print("status got removed...")
f = Flight("CA980")
f.flight_status
f.flight_status =2 #觸發(fā)@flight_status.setter
#del f.flight_status #觸發(fā)@flight_status.deleter