Skip to content

Commit 087f395

Browse files
author
l
committed
add encapsulation stretch task
1 parent a778b38 commit 087f395

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎sprint-5/09-encapsulation.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Person:
2+
def __init__(self, name: str, age: int):
3+
self.name = name
4+
self.__age = age
5+
6+
def is_adult(self):
7+
return self.__age >= 18
8+
9+
imran = Person("Imran", 22)
10+
print(imran.name)
11+
print(imran.age) # fails
12+
print(imran.__age) # fails
13+
print(imran.is_adult()) # works and prints True
14+
15+
eliza = Person("Eliza", 12)
16+
print(eliza.name)
17+
print(eliza.age) # fails
18+
print(imran.__age) # fails
19+
print(eliza.is_adult()) # works and prints False
20+
21+
# Task 9
22+
# Read through Python encapsulation
23+
# https://www.w3schools.com/python/python_encapsulation.asp
24+
# and think about some of the benefits that encapsulation can add to a class.
25+
#
26+
# Do some further research of your own to learn about encapsulation.
27+
#
28+
# Think of some examples and in your own words write down some benefits and trade-offs of using encapsulation in classes:
29+
#
30+
#
31+
#
32+
33+
# Stretch Task
34+
# Make the name property private, and add a get_name() method to make it read only.

‎sprint-5/09-encapsulation.txt‎

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)