在 Python 中,Class(類別) 是一種用來'封裝數據'和'操作數據方法'的結構。
Class 提供了創建自定義數據類型的能力,可以用來模擬實際世界的物件或場景。
「實例化後的產物」指的是當我們使用類(Class)創建出物件(Object)時生成的具體實例。實例化是一個過程,把類這個模板具體化,生成能實際使用的物件。每個物件都是類的實例(Instance)。
簡單解釋:類與物件的關係
類(Class)是一個概念、一個模板,就像建築圖紙。
實例化(Instantiation)就是用這個模板製造出一個具體的東西。
物件(Object)是實例化後的產物,就像按圖紙建造出來的建築。
class ClassName:
    def __init__(self, attribute1, attribute2):
        self.attribute1 = attribute1  # 定義屬性
        self.attribute2 = attribute2
    def method(self):
        return f"Attribute1 is {self.attribute1}, Attribute2 is {self.attribute2}"
使用類來建立物件 Class 時會呼叫建構式Constructor並賦於屬性值
__init__就是類的「建構子」(constructor),但不像某些語言的建構子會直接建立物件,而是用來初始化物件。
Python 中的建構式 (Constructor)是類的 init 方法,用於初始化物件的屬性。
** 建構式的功能 **
** 建構式的注意事項 **
class Person:
    # 初始化方法 (__init__)
    def __init__(self, name, age):
        self.name = name  # 設定物件的屬性 name
        self.age = age    # 設定物件的屬性 age
    # 定義方法
    def greet(self):
        return f"Hello, my name is {self.name} and I am {self.age} years old."
person1 = Person("Alice", 25)  # 創建 Person 類的實例
person2 = Person("Bob", 30)
# 使用物件屬性
print(person1.name)  # 輸出: Alice
# 呼叫物件方法
print(person2.greet())  # 輸出: Hello, my name is Bob and I am 30 years old.
在 Python 中,如果某個屬性是所有物件(實例)都需要‘共享’的,那麼可以使用**類屬性(Class Attribute)**來處理。類屬性是屬於類本身的,所有該類的實例共享同一個值,而不是每個實例擁有自己的拷貝。
如何設置類屬性
類屬性是在類中定義的變數,通常在類的定義中直接設置,而不是在 init 方法中。
實例可以訪問類屬性,但類屬性並不屬於個別的實例。
class Dog:
    # 類屬性
    species = "Canis Familiaris"  # 所有狗的共同屬性
    def __init__(self, name, breed):
        # 實例屬性
        self.name = name
        self.breed = breed
# 創建物件
dog1 = Dog("Buddy", "Golden Retriever")
dog2 = Dog("Max", "German Shepherd")
# 訪問類屬性
print(dog1.species)  # 輸出: Canis Familiaris
print(dog2.species)  # 輸出: Canis Familiaris
# 修改類屬性
Dog.species = "Canis Lupus Familiaris"
# 所有實例都會看到改動
print(dog1.species)  # 輸出: Canis Lupus Familiaris
print(dog2.species)  # 輸出: Canis Lupus Familiaris
class Animal:
    def __init__(self, species):
        self.species = species
    def sound(self):
        return "Some generic sound"
class Dog(Animal):
    def __init__(self, name, species="Dog"):
        super().__init__(species)  # 繼承父類別的初始化
        self.name = name
    def sound(self):
        return "Woof!"  # 覆蓋父類別的方法
dog = Dog("Buddy")
print(dog.sound())  # 輸出: Woof!
super() 是一個內建函數,用於調用**父類(或基本base類)**的方法
關鍵點:
class Example:
    def __init__(self):
        self.__private_attr = "Private"
    def __private_method(self):
        return "This is private"
    def public_method(self):
        return f"Accessing: {self.__private_attr}"
example = Example()
print(example.public_method())  # 可透過公有方法訪問私有屬性
Private 的用途
多重繼承 是指一個類別可以同時繼承多個父類別的特性和行為。在 Python 中,多重繼承是一種允許類別設計更靈活的方法,但也需要謹慎使用以避免複雜性問題。
class Animal:
    def eat(self):
        print("This animal can eat.")
class Flyable:
    def fly(self):
        print("This animal can fly.")
class Bird(Animal, Flyable):  # 同時繼承 Animal 和 Flyable
    def chirp(self):
        print("Bird is chirping.")
# 使用 Bird 類別
bird = Bird()
bird.eat()  # 來自 Animal 類別
bird.fly()  # 來自 Flyable 類別
bird.chirp()  # 來自 Bird 類別
輸出:
This animal can eat.
This animal can fly.
Bird is chirping.
優點
行為組合:可以將不同父類別的功能組合在一起,形成更強大的子類別。
代碼重用:避免重複撰寫通用功能。
更靈活的設計:允許將不同責任分散到多個類別中,並在子類別中合併。
方法解析順序 (MRO, Method Resolution Order)