發表文章

目前顯示的是 11月, 2025的文章

Class Variables 演練與變數記憶體關係範例

圖片
class Counter:      total = 0      def __init__(self):           Counter.total += 1 a = Counter() b = Counter() c = Counter() print(Counter.total) 測試重點詳解: 類別變數 total 的定義與初始化: total = 0 定義在 class Counter: 區塊內,而不是在任何方法(如 __init__)中,這使得 total 成為一個類別變數。 特性: 所有透過 Counter 類別建立的物件(實例,如 a、b、c)都會共享這同一個 total 變數。 __init__ 構造函數中的操作: def __init__(self): 是在物件被建立時自動呼叫的方法。 Counter.total += 1 確保了每當一個 Counter 類別的物件被建立時,共享的類別變數 total 的值就會增加 1。 程式碼執行過程: a = Counter():建立第一個物件,Counter.total 變為 0 + 1 = 1。 b = Counter():建立第二個物件,Counter.total 變為 1 + 1 = 2。 c = Counter():建立第三個物件,Counter.total 變為 2 + 1 = 3。 最終輸出: print(Counter.total) 最終會輸出 3。 但是,更進階一點,如果程式寫成下面的方式 class Counter:     total = 0     def __init__(self):           print(f"Counter.total={Counter.total}, id={id(Counter.total)}")          Counter.total += 1         self.total += 2     ...