一、內置裝飾器
| 內置裝飾器 | 含義 |
|---|---|
| classmethod | 類方法 |
| staticmethod | 靜態方法 |
二、普通方法(回顧)
-
定義:
-
第一個引數為self,代表 實體本身
-
-
呼叫:
-
要有實體化的程序,通過 實體物件.方法名 呼叫
# 1. 類的定義 class MethodClass: class_param = 0 # 類變數 def __init__(self): # 實列變數 self.a = 'abc' def demo_method(self): print('這是一個普通方法') def demo_method2(self): self.demo_method() self.a = 'acb' print('這是一個普通方法') # 定義類方法必須加 classmethod裝飾器 @classmethod def class_method(cls): # 類方法,第一個引數需要改為cls # cls.demo_method() 類方法內,不可以直接呼叫實列方法 # cls.a 類方法內,不可以直接呼叫實列變數 cls.class_method2() # 類方法內,可以直接呼叫類變數與類方法 print('這是一個類方法', cls.class_param) # 類變數是可以直接呼叫的 @classmethod def class_method2(cls): # 類方法,第一個引數需要改為cls print('這是一個類方法2', cls.class_param) # 呼叫類方法 MethodClass.class_method() # 無需實體化,直接通過 類.方法名 呼叫,也可以通過 實體.方法名 呼叫 # 實體化呼叫 demo = MethodClass() demo.demo_method() # 在呼叫程序中,類和實列都可以直接呼叫類方法 # 呼叫普通方法,需要實體化,要不然會報錯 # MethodClass.demo_method()
三、靜態方法
-
定義:
-
使用 @staticmethod 裝飾器,沒有和類本身有關的引數
-
無法直接使用任何類變數、類方法或者實體方法、實體變數
-
-
呼叫:
-
無需實體化,直接通過 類.方法名 呼叫,也可以通過 實體.方法名 呼叫
# 1. 定義 class MethodsDemo: param_a = 0 # 定義類變數 def demo_method(self): # 普通方法 print('這是一個普通方法') @classmethod def class_method2(cls): # 類方法,第一個引數需要改為cls print('這是一個類方法2') @staticmethod # 定義靜態方法 必須加@staticmethod裝飾器 def static_demo(p): """ 靜態方法 :return: """ # self./cls. 不可以使用,沒有這個引數 print("靜態方法", p) # 無法直接呼叫類變數 # 2. 類呼叫 MethodsDemo.static_demo(5) # 實列物件呼叫 demo = MethodsDemo() demo.static_demo(5)
四、普通方法、類方法、靜態方法的區別
| 名稱 | 定義 | 呼叫 | 關鍵字 | 使用場景 |
|---|---|---|---|---|
| 普通方法 | 至少需要一個引數self | 實體名.方法名() | 無 | 方法內部涉及到實體物件屬性的操作 |
| 類方法 | 至少需要一個cls引數 | 類名.方法名() 或者實體名.方法名() | @classmethod | 如果需要對類屬性,即靜態變數進行限制性操作 |
| 靜態方法 | 無默認引數 | 類名.方法名() 或者實體名.方法名() | @staticmethod | 無需類或實體參與 |
五、類方法實際案例
1、下面的代碼實作的需求是格式化輸出時間如果現在需求變更,輸入 年、月、日 沒法保證格式統一,可能是json,可能是其他格式的字串,在不修改建構式的前提下,如何更改代碼?
class DateFormat: def __init__(self, year=0, month=0, day=0): self.year = year self.month = month self.day = day def out_date(self): return f"輸入的時間為{self.year}年,{self.month}月,{self.day}日" year, month, day = 2017, 7, 1 demo = DateFormat(year, month, day) print(demo.out_date())
class DateFormat: def __init__(self, year=0, month=0, day=0): self.year = year self.month = month self.day = day def out_date(self): return f"輸入的時間為{self.year}年,{self.month}月,{self.day}日" @classmethod def json_formate(cls, json_date): """ 輸入一個字典格式的資料型別,回傳(2021,12,17) """ year, month, day = json_date['year'], json_date['month'], json_date['day'] return cls(year, month, day) json_data = {'year': 2021, 'month': 12, 'day': 17} # 使用json格式化,生成想要的日期格式,回傳DateFormat實列 demo =DateFormat.json_formate(json_data) print(demo.out_date())
六、靜態方法實際案例
-
此方法沒有任何和實體、類相關的部分,可以作為一個獨立函式使用
-
某些場景下,從業務邏輯來說又屬于類的一部分
""" 多輪比賽,每輪由不同的英雄對打 """ class Game: def __init__(self, first_hero, second_hero): self.first_hero = first_hero self.second_hero = second_hero # fight 有和實列變數互動的部分,所有需要定義為一個普通方法 def fight(self): print(f'本輪比賽開始,由{self.first_hero}vs{self.second_hero}') # start 沒有和類或實列互動的部分,那么就可以使用@staticmethod @staticmethod def start(): print('游戲開始') Game.start() game1 = Game("Bob", "Jack") game2 = Game('Tom', 'Alice') game1.fight() game2.fight()
本文來自博客園,作者:{jiuyou-emperor},轉載請注明原文鏈接:{https://www.cnblogs.com/jiuyou-emperor/}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/412908.html
標籤:其他
上一篇:當類的泛型相關時,如何在兩個泛型類之間創建類似子型別的關系
下一篇:不卷了!技術團隊成員集體辭職
