所有匹配主题的 Gist abc

timmy / 使用 ABC 定義抽象類別

0 喜欢
0 派生
1 文件
最后活跃于 9 months ago
ABC(Abstract Base Class)允許定義抽象類別,強制子類別實作特定方法,適用於建立統一的介面規範,確保繼承的類別都遵守特定的行為。
1 from abc import ABC, abstractmethod
2
3 # 定義抽象類別
4 class Animal(ABC):
5 @abstractmethod
6 def make_sound(self) -> str:
7 """所有動物都必須實作此方法"""
8 pass
9
10 # 繼承並實作抽象方法
上一页 下一页