ComeçarComece de graça

Building a formal interface with ABC

Formal interfaces offer a more enforceable contract between an interface and any classes that implement it. You'll practice creating a formal interface made up of two abstract methods, using the abc module. The ABC class and abstractmethod decorator have been imported from abc. Have fun!

Este exercício faz parte do curso

Intermediate Object-Oriented Programming in Python

Ver curso

Instruções do exercício

  • Create a formal interface, called Product.
  • Define purchase() as an abstract method with parameters self and quantity, and add the pass keyword to the method body.
  • Define the update_price() abstract method, which takes parameters self and new_price.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Create a Product interface
____ ____(____):
  
  # Define a purchase() abstract method
  ____
  def ____(____, quantity):
    ____
  
  # Create an update_price() abstract method
  ____
  def ____(self, ____):
    ____
Editar e executar o código