...
/Solution Review: Implement Rectangle Class Using Encapsulation
Solution Review: Implement Rectangle Class Using Encapsulation
This review provides a detailed explanation for the 'Implement the Rectangle Class using the Concepts of Encapsulation' challenge.
We'll cover the following...
Solution #
Press + to interact
class Rectangle:def __init__(self, length, width):self.__length = lengthself.__width = widthdef area(self):return (self.__length * self.__width)def perimeter(self):return (2 * (self.__length + self.__width))obj1 = Rectangle(4, 5)print("Area is", obj1.area())print("Perimeter is", obj1.perimeter())
Explanation
-
In lines 3 - 4, we defined the initializer for the class and declared private properties –
__length
and__width
– in it. -
In line 7 ...