Informal interfaces
Informal interfaces provide a set of methods that must be defined in all classes that implement that interface. Below is a class named Supplier that will act as an informal interface. In order for YogurtSupplier to fulfill the contract set out by the Supplier interface, it must define the methods take_order() and make_delivery(). In this exercise, you'll practice doing just that.
class Supplier:
def take_order(self, product_name, quantity):
pass
def make_delivery(self, order_id, location):
pass
Este exercício faz parte do curso
Intermediate Object-Oriented Programming in Python
Instruções do exercício
- In the
YogurtSupplierclass, define thetake_order()method to add an order to theself.ordersdictionary. - Complete the contract set out by the
Supplierinterface by defining amake_delivery()method to print a message and delete the order stored usingorder_idfromself.orders.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
class YogurtSupplier:
def __init__(self):
self.orders = {}
# Finish defining the take_order() method
____:
self.____[f"{product_name}_{quantity}"] = {
"product_name": ____, "quantity": ____
}
# Implement a make_delivery() abstract method
____:
print(f"Delivering order: {order_id} to {location}")
del ____.____[____]