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.
Bu egzersiz
Introduction to Object-Oriented Programming in Python
kursunun bir parçasıdırEgzersiz talimatları
- Define a
Calculatorclass with two parameters on initialization:num_oneandnum_two. - Define an
addition()method, which returnsself.num_oneplusnum_two. - Define an
subtraction()method, which returnsself.num_oneminusnum_two. - Define an
multiplication()method, which returnsself.num_onemultiplied bynum_two.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# 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 ____(____):
____