การ implement formal interface ด้วย ABC
ในแบบฝึกหัดนี้ จะได้ฝึก implement formal interface ที่สร้างขึ้นโดยใช้คลาส ABC และ decorator @abstractmethod โดย interface ชื่อ Business ได้เตรียมไว้ให้แล้ว ดังที่แสดงด้านล่าง
class Business(ABC):
@abstractmethod
def sell_product(self, product_name, price, quantity):
pass
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Object-Oriented Programming ใน Python ระดับกลาง
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Create a class that inherits the Business interface
class ____(____):
def __init__(self, business_name):
self.business_name = business_name
# Attempt to create a Bakery object, observe the exception
____:
blue_eyed_baker = ____("Blue Eyed Baker")
____ Exception ____ e:
print(____)