扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
#静态方法用的少,相当于类的工具包,访问不了类或实例中的任何属性
classDog(object):
def__init__(self,name):
self.name=name
@staticmethod#实际上跟类没关系了,就只是一个函数了,名义上归类管
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函数,使self有效
d.talk()
成都创新互联拥有一支富有激情的企业网站制作团队,在互联网网站建设行业深耕10余年,专业且经验丰富。10余年网站优化营销经验,我们已为上1000家中小企业提供了成都网站建设、成都网站制作解决方案,按需设计,设计满意,售后服务无忧。所有客户皆提供一年免费网站维护!
#类方法基本用不到
class Dog(object):
name="huazai"
def __init__(self,name):
self.name=name
self.n=2222
@classmethod #类方法只能访问类变量,不能访问实例变量
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 #方法转属性(函数转变量)
defeat(self):
print("%s is eating %s"%(self.name,self.__food))#先传一个空变量 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函数 del self.__food删除self.__food变量
d.eat #依然调用@property下的eat 提示删除了Dog.__food变量 即self.__food
# @property
#def eat(self):
# print("%s is eating %s"%(self.name,self.__food))
#属性方法实例
classFlight(object):
def__init__(self, name):
self.flight_name = name
defchecking_status(self):
print("checking flight %s status "%self.flight_name)
return2
@property #航班状态方法转变成属性方法
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 #修改航班状态
defflight_status(self, status):
print("flight %s has changed the flight status to %s"%(self.flight_name,status))
@flight_status.deleter #删除航班状态
defflight_status(self):
print("status got removed...")
f = Flight("CA980")
f.flight_status
f.flight_status =2 #触发@flight_status.setter
#del f.flight_status #触发@flight_status.deleter
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流