시작하기무료로 시작하기

비형식적 인터페이스

비형식적 인터페이스는 해당 인터페이스를 구현하는 모든 클래스에서 반드시 정의해야 하는 메서드 집합을 제공합니다. 아래에는 비형식적 인터페이스 역할을 하는 Supplier 클래스가 있습니다. YogurtSupplierSupplier 인터페이스가 제시한 계약을 이행하려면, take_order()make_delivery() 메서드를 정의해야 합니다. 이번 연습에서는 바로 이것을 실습해 보겠습니다.

class Supplier:
  def take_order(self, product_name, quantity):
    pass

  def make_delivery(self, order_id, location):
    pass

이 연습은 강의의 일부입니다

Python 중급 객체 지향 프로그래밍

강의 보기

연습 안내

  • YogurtSupplier 클래스에서 take_order() 메서드를 정의해 주문을 self.orders 딕셔너리에 추가하세요.
  • Supplier 인터페이스가 제시한 계약을 완성하도록 make_delivery() 메서드를 정의해 메시지를 출력하고, order_id로 저장된 주문을 self.orders에서 삭제하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

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 ____.____[____]
코드 편집 및 실행