屬性:python
公有屬性 (屬於類,每一個類一份)函數
普通屬性 (屬於對象,每一個對象一份)orm
私有屬性 (屬於對象,跟普通屬性類似,只是不能經過對象直接訪問) 對象
方法:(按做用)blog
構造方法rem
析構函數get
方法:(按類型)it
普通方法自動化
私有方法(方法前面加兩個下劃線)ast
靜態方法
類方法
屬性方法
@staticmethod
靜態方法,經過類直接調用,不須要建立對象,不會隱式傳遞self
@classmethod
類方法,方法中的self是類自己,調用方法時傳的值也必須是類的公有屬性,
就是說類方法只能操做類自己的公有字段
class Dog(object): food = "gutou" age = "1" def __init__(self, name): self.NAME = name @classmethod def eat(self,age): #只能是類中的變量 # print(self.NAME) print(age) print(self.food) @classmethod def eat1(self, age): # 只能是類中的變量 # print(self.NAME) age = "2" self.food = "tang" @staticmethod def print_1(): print(Dog.food, Dog.age) d = Dog("labuladuo") d.eat(Dog.age) #經過對象調用 Dog.eat(Dog.age) #經過類調用 print("-----1-----") d.eat1(Dog.age) Dog.print_1() print("--------2-------") Dog.eat1(Dog.age) Dog.print_1()
屬性變爲私有屬性,加斷言
class Cycle(object): def __init__(self,x,y,radius): self.x = x self.y = y self.radius = radius @property def radius(self): return self.__radius @radius.setter def radius(self,radius): assert radius > 0, "radius must be nonzero and non-negative" self.__radius = radius @radius.deleter def radius(self): del self.__radius def __str__(self): return "({0},{1},{2})".format(self.x,self.y,self.radius) c = Cycle(1,1,7) c.radius = 9 print(c) del c.radius print(c.radius) #(1,1,9) #AttributeError: 'Cycle' object has no attribute '_Cycle__radius'
class Dog(object): def __init__(self, name): self.name = name self.__food = None # def eat(self, food):原始方式 # self.__food = food # print('%s eat %s' %(self.name, food)) @property def eat(self): print('%s eat %s' %(self.name,self.__food)) @eat.setter def eat(self, food): self.__food = food @eat.deleter def eat(self): del self.__food print("刪完了") d = Dog("labuladuo") # d.eat("baozi") #原始方式 d.eat d.eat = "baozi" d.eat #調用方式沒變,只是改變傳入的參數就改變告終果 #能夠刪除__food屬性 del d.eat d.eat # 報錯 '''輸出 labuladuo eat None labuladuo eat baozi 刪完了 AttributeError: 'Dog' object has no attribute '_Dog__food' '''
屬性方法應用場景
好吧,把一個方法變成靜態屬性有什麼卵用呢?既然想要靜態變量,那直接定義成一個靜態變量不就得了麼?well, 之後你會需到不少場景是不能簡單經過 定義 靜態屬性來實現的, 好比 ,你想知道一個航班當前的狀態,是到達了、延遲了、取消了、仍是已經飛走了, 想知道這種狀態你必須經歷如下幾步:
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") f = Flight("CA980") f.flight_status
cool , 那如今我只能查詢航班狀態, 既然這個flight_status已是個屬性了, 那我可否給它賦值呢?試試吧
f = Flight("CA980") f.flight_status f.flight_status = 2
輸出爲:發現不能更改
checking flight CA980 status flight is arrived... Traceback (most recent call last): File "/Users/jieli/PycharmProjects/python基礎/自動化day7面向對象高級/屬性方法.py", line 58, in <module> f.flight_status = 2 AttributeError: can't set attribute
固然能夠改, 不過須要經過@proerty.setter裝飾器再裝飾一下,此時 你須要寫一個新方法, 對這個flight_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 = { : "canceled", :"arrived", : "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