Đóng gói là một trong những khái niệm cơ bản trong lập trình hướng đối tượng [OOP]. Nó mô tả ý tưởng gói dữ liệu và các phương thức hoạt động trên dữ liệu trong một đơn vị. Điều này đặt các hạn chế đối với việc truy cập các biến và phương pháp trực tiếp và có thể ngăn chặn việc sửa đổi dữ liệu tình cờ. Để ngăn chặn sự thay đổi tình cờ, một biến số đối tượng chỉ có thể được thay đổi bằng phương pháp đối tượng. Những loại biến được gọi là biến riêng.private variables.
Một lớp là một ví dụ về đóng gói khi nó gói gọn tất cả các dữ liệu là chức năng thành viên, biến, v.v.
Hãy xem xét một ví dụ thực tế về đóng gói, trong một công ty, có các phần khác nhau như phần tài khoản, phần tài chính, phần bán hàng, v.v ... Phần tài chính xử lý tất cả các giao dịch tài chính và lưu giữ hồ sơ của tất cả các dữ liệu liên quan đến tài chính. Tương tự, phần bán hàng xử lý tất cả các hoạt động liên quan đến bán hàng và lưu giữ hồ sơ của tất cả các doanh số. Bây giờ có thể phát sinh một tình huống khi một số lý do, một quan chức từ phần Tài chính cần tất cả dữ liệu về bán hàng trong một tháng cụ thể. Trong trường hợp này, anh ta không được phép truy cập trực tiếp dữ liệu của phần bán hàng. Trước tiên anh ta sẽ phải liên hệ với một số nhân viên khác trong phần bán hàng và sau đó yêu cầu anh ta cung cấp dữ liệu cụ thể. Đây là những gì đóng gói là. Ở đây, dữ liệu của phần bán hàng và các nhân viên có thể thao túng họ được gói dưới một tên duy nhất của phần bán hàng. Sử dụng đóng gói cũng che giấu dữ liệu. Trong ví dụ này, dữ liệu của các phần như bán hàng, tài chính hoặc tài khoản được ẩn khỏi bất kỳ phần nào khác.
Thành viên được bảo vệ
Các thành viên được bảo vệ [trong C ++ và Java] là những thành viên của lớp không thể truy cập bên ngoài lớp nhưng có thể được truy cập từ bên trong lớp và các lớp con của nó. Để thực hiện điều này trong Python, chỉ cần làm theo quy ước bằng cách tiền tố tên của thành viên bằng một bản nhấn mạnh duy nhất.the convention by prefixing the name of the member by a single underscore “_”.
Mặc dù biến được bảo vệ có thể được truy cập ra khỏi lớp cũng như trong lớp dẫn xuất [được sửa đổi trong lớp dẫn xuất], nhưng thông thường [quy ước không phải là quy tắc] để không truy cập vào cơ thể lớp được bảo vệ.
Lưu ý: Phương thức __init__ là một hàm tạo và chạy ngay khi một đối tượng của một lớp được khởi tạo. & Nbsp; & nbsp; The __init__ method is a constructor and runs as soon as an object of a class is instantiated.
Python3
class
Base:
def
__init__[
self
GeeksforGeeks0
self
__1212 GeeksforGeeks4
class
GeeksforGeeks6
def
__init__[
self
GeeksforGeeks0
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'3
self
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'5
GeeksforGeeks0
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'7
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'8
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'9
class
0class
1self
class
3
GeeksforGeeks0
self
__1212 GeeksforGeeks4
GeeksforGeeks0
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'7
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'8
Base:
2class
0class
1self
class
3
class
GeeksforGeeks6
GeeksforGeeks0
self
__1212 class
8Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'7
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'8
5
6Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'7
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'8
9def
0Output:
Calling protected member of base class: 2 Calling modified protected member outside class: 3 Accessing protected member of obj1: 3 Accessing protected member of obj2: 2
Base:
7GeeksforGeeks
3 Base:
9
0
GeeksforGeeks3
2Private instance variables that cannot be accessed except inside a class.Thành viên tư nhân
Các thành viên tư nhân tương tự như các thành viên được bảo vệ, sự khác biệt là các thành viên trong lớp tuyên bố riêng tư không nên được truy cập bên ngoài lớp cũng như bởi bất kỳ lớp cơ sở nào. Trong Python, không có sự tồn tại của các biến thể hiện riêng không thể truy cập ngoại trừ bên trong một lớp. Python’s private and protected members can be accessed outside the class through python name mangling.
Python3
class
Base:
def
__init__[
self
GeeksforGeeks0
self
__1212 GeeksforGeeks4
class
GeeksforGeeks6
class
GeeksforGeeks6
def
__init__[
self
GeeksforGeeks0
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'3
self
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'5
GeeksforGeeks0
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'7
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'8
]:
2Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'5
GeeksforGeeks0
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'7
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'8
self
]:
8GeeksforGeeks0
self
__1212 GeeksforGeeks4
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'7
GeeksforGeeks03
Output:
GeeksforGeeks
Traceback [most recent call last]: File "/home/f4905b43bfcf29567e360c709d3c52bd.py", line 25, in print[obj1.c] AttributeError: 'Base' object has no attribute 'c' Traceback [most recent call last]: File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 27, in obj2 = Derived[] File "/home/4d97a4efe3ea68e55f48f1e7c7ed39cf.py", line 20, in __init__ print[self.__c] AttributeError: 'Derived' object has no attribute '_Derived__c'