python(属性方法@property)

# Author:Sooele
class Flight(object):   #飞机航班的类
    def __init__(self,name):
        self.flight_name = name   #实例化只需要传航班的名字


    def checking_status(self):   #检查航班的状态
        print("checking flight %s status " % self.flight_name)
        return  2           ###查询航班返回的状态码

    @property   #属性方法
    def flight_status(self):   #航班状态
        status = self.checking_status()  #先检查航班状态
        if status == 0 :  ##航班返回的状态码
            print("flight got cancel...")  #如果是0,飞机取消了
        elif status == 1 : ##航班返回的状态码
            print("flight is arrived...")  #如是是1飞机到达了
        elif status == 2: ##航班返回的状态码
            print("flight has departured already...") #如果是2,飞机已经离开了
        else:
            print("cannot confirm the flight status...,please check later")  #如果没有,状态无法查询

    @flight_status.setter    #修改航班状态码属性方法
    def flight_status(self,status):  #f.flight_status = 2
        print("flight %s has changed status to %s" %(self.flight_name,status))

f = Flight("CA980")
f.flight_status
f.flight_status = 2

发表评论