-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncap.py
More file actions
50 lines (47 loc) · 1.37 KB
/
Encap.py
File metadata and controls
50 lines (47 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Here we cover encapsulation
# Encapuslation use for data hiding,and secure our data
class Bank:
def __init__(self,name,balance):
self.__name = name
self.__balance = balance
obj=Bank("Mudassar", 1000)
try:
print(obj.__name)
print('You can access the data')
except Exception as e:
print(e)
print('You can not access the data')
print('\n')
print(obj._Bank__balance)
print('You can access the data')
# now we make an ATM machine and also we use Encapsulation here
class ATM_Machine:
def __init__(self,name,balance,withdraw,deposit):
self.name = name
self.__balance = balance
self.withdraw = withdraw
self.__deposit = deposit
def withdraw(self,amount):
if self.__balance >= amount:
self.__balance -= amount
print("Withdraw successful")
else:
print("Insufficient balance")
def deposit(self):
self.__balance += self.__deposit
print("Deposit successful")
def get_balance(self):
return self.__balance
def get_name(self):
return self.name
def get_withdraw(self):
return self.withdraw
def get_deposit(self):
return self.deposit
obj = ATM_Machine("Mudassar", 10000)
print(obj.get_balance())
print(obj.get_name())
obj.set_balance(2000)
obj.set_name("Mudassar Ali")
print(obj.get_balance())
print(obj.get_name())