Aan de slagGa gratis aan de slag

Building a class from scratch

In the last exercise of the chapter, you'll step into the role of a Python developer and build your own class from scratch including a constructor!

You'll design a Calculator class, including methods that enable the addition, subtraction, or multiplication of two values.

Deze oefening maakt deel uit van de cursus

Introduction to Object-Oriented Programming in Python

Cursus bekijken

Oefeninstructies

  • Define a Calculator class with two parameters on initialization: num_one and num_two.
  • Define an addition() method, which returns self.num_one plus num_two.
  • Define an subtraction() method, which returns self.num_one minus num_two.
  • Define an multiplication() method, which returns self.num_one multiplied by num_two.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Define and initialize the Calculator class
____:
  ____ ____(____, ____, ____):
    self.____ = ____
    self.____ = ____
  
  # Create the addition method
  def ____(____):
    return self.____ ____ self.____
  
  # Create the subtraction method
  def ____(____):
    return ____
  
  # Create the multiplication method
  def ____(____):
    ____   
Code bewerken en uitvoeren