Electron Is

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

Got it!

If you want to use encapsulation and make the attributes private by adding
double underscores (`__`) after `self`, you can do so. Here's the corrected code
with encapsulation:

```python
class Room:
def __init__(self, number, floor):
self.__number = number # Private attribute
self.__floor = floor # Private attribute

def access_room_number(self):
return self.__number

def access_room_floor(self):
return self.__floor

# Creating an instance of Room


myroom = Room(102, 2)

# Correctly calling the method and printing the result


print(myroom.access_room_floor()) # Output: 2
```

In this version, `__number` and `__floor` are private attributes, and they can only
be accessed within the `Room` class. The methods `access_room_number` and
`access_room_floor` provide controlled access to these private attributes. This
way, you achieve encapsulation, which helps in protecting the internal state of the
`Room` object.

You might also like