Alle Neu

user:thomas Gists erstellt von Nutzer

title:mygist Gists mit Titel

filename:myfile.txt Gists mit Dateinamen

extension:yml Gists mit Dateiendung

language:go Gists in Sprache

topic:homelab gists with given topic

Anmelden


Alle Neu Anmelden

All gists matching topic interface

Neueste erstellt
Älteste erstellt
Neueste bearbeitet
Älteste bearbeitet

timmy / 簡單實作資料庫依賴注入

0 Favoriten
0 Forks
1 Dateien
Zuletzt aktiv 7 months ago
用 Python 實現資料庫介面,輕鬆切換不同資料庫實作。
abc-class database dependency-injection interface mysql oop python
1 from abc import ABC, abstractmethod
2
3 # 定義一個資料庫介面,規定大家都要有 connect 方法
4 class IDatabase(ABC):
5 @abstractmethod
6 def connect(self):
7 pass
8
9 # 具體實作 MySQL 版資料庫
10 class MySQLDatabase(IDatabase):

timmy / 使用 ABC 定義抽象類別

0 Favoriten
0 Forks
1 Dateien
Zuletzt aktiv 9 months ago
ABC(Abstract Base Class)允許定義抽象類別,強制子類別實作特定方法,適用於建立統一的介面規範,確保繼承的類別都遵守特定的行為。
abc abstract-class interface oop python
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 # 繼承並實作抽象方法
Neuer Älter

Powered by Opengist ⋅ Load: 33ms⋅

Deutsch
Čeština Deutsch English Español Français Magyar Italiano 日本語 Polski Português Русский Türkçe Українська 中文 繁體中文