1. 程式人生 > >Python學習之路:staticmethod classmethod property方法

Python學習之路:staticmethod classmethod property方法

屬性 -c name .html 學習 assm true urn 系列

參考鏈接:http://www.cnblogs.com/alex3714/articles/5213184.html

靜態方法

  只是名義上歸類管理,實際上在靜態方法裏訪問不了類或實例中的任何屬性

class Dog(object):
    def __init__(self,name):
        self.name = name

    @staticmethod #實際上跟類沒什麽關系了
    def eat(self):
        print("%s is eating %s"%(self.name,‘dd‘))

    def talk(self):
        print("%s is talking"%self.name)


d = Dog("ChenRH")
d.eat(d)
d.talk()

類方法:

  只能訪問類變量,不能訪問實例變量

class Dog(object):
    #n = 3333
    name = "Huazai"
    def __init__(self,name):
        self.name = name

    #@staticmethod #實際上跟類沒什麽關系了
    @classmethod
    def eat(self):
        print("%s is eating %s"%(self.name,‘dd‘))

    def talk(self):
        print("%s is talking"%self.name)


d = Dog("ChenRH")
d.eat()
d.talk()

屬性方法:

  把一個方法變成一個靜態屬性

class Dog(object):
    #n = 3333
    name = "Huazai"
    def __init__(self,name):
        self.name = name

    #@staticmethod #實際上跟類沒什麽關系了
    #@classmethod
    @property #attribute
    def eat(self):
        print("%s is eating %s"%(self.name,‘dd‘))

    # def talk(self):
    #     print("%s is talking"%self.name)


d = Dog("ChenRH")
d.eat

class Dog(object):
    #n = 3333
    name = "Huazai"
    def __init__(self,name):
        self.name = name
        self.__food =None

    #@staticmethod #實際上跟類沒什麽關系了
    #@classmethod
    @property #attribute,把一個方法變成屬性
    def eat(self):
        print("%s is eating %s"%(self.name,self.__food))


    @eat.setter#修改屬性
    def eat(self,food):
        print("set to food:",food)
        self.__food = food

    @eat.deleter#刪除私有屬性
    def eat(self):
        del self.__food
        print("刪完了")

    # def talk(self):
    #     print("%s is talking"%self.name)


d = Dog("ChenRH")
d.eat
d.eat = "baozi"

del d.eat

靜態屬性方法適用場景舉例:

你想知道一個航班當前的狀態,是到達了、延遲了、取消了、還是已經飛走了, 想知道這種狀態你必須經歷以下幾步:

1. 連接航空公司API查詢

2. 對查詢結果進行解析

3. 返回結果給你的用戶

因此這個status屬性的值是一系列動作後才得到的結果,所以你每次調用時,其實它都要經過一系列的動作才返回你結果,但這些動作過程不需要用戶關心, 用戶只需要調用這個屬性就可以,明白 了麽?

class Flight(object):
    def __init__(self,name):
        self.flight_name = name


    def checking_status(self):
        print("checking flight %s status " % self.flight_name)
        return  1


    @property
    def flight_status(self):
        status = self.checking_status()
        if status == 0 :
            print("flight got canceled...")
        elif status == 1 :
            print("flight is arrived...")
        elif status == 2:
            print("flight has departured already...")
        else:
            print("cannot confirm the flight status...,please check later")

    @flight_status.setter #修改
    def flight_status(self,status):
        status_dic = {
            0 : "canceled",
            1 :"arrived",
            2 : "departured"
        }
        print("\033[31;1mHas changed the flight status to \033[0m",status_dic.get(status) )

    @flight_status.deleter  #刪除
    def flight_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 

Python學習之路:staticmethod classmethod property方法