-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencapsulationpython.py
More file actions
47 lines (36 loc) · 1.39 KB
/
encapsulationpython.py
File metadata and controls
47 lines (36 loc) · 1.39 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
# class Employee:
# def __init__(self, name, dept, salary):
# self.name = name
# self.dept = dept
# self.salary = salary
# def show(self):
# print(f'I am {self.name} working in {self.dept} for Rs.{self.salary}')
# e1 = Employee('ABC', 'PQR', 150000)
# e2 = Employee('XYZ', 'PQR', 130000)
# e1.show() # Encapculation at class level
# e2.show() # Encapculation at class level
# Access specifiers
class Employee:
def __init__(self, name, dept, salary):
self.name = name #public access
self._dept = dept #protected access
self.__salary = salary # private access
# Public Method to access the data members
def show(self):
print(f'I am {self.name} woking in {self._dept} for Rs. {self.__salary}')
# Protected Method to access the data members
def _display(self):
print(f'I am {self.name} woking in {self._dept} for Rs. {self.__salary}')
# Private Method to access the data members
def __see(self):
print(f'I am {self.name} woking in {self._dept} for Rs. {self.__salary}')
e1 = Employee('ABC', 'HR', 150000)
# --- Accessing data members from outside the class
# Use the object and the data member
print(e1.name)
print(e1._dept)
# print(e1.__salary) # This line of code throws an error
# Use public method to access the data from outside of the class
e1.show()
# Use Name Mangling
print(e1._Employee__salary)