Hướng dẫn encapsulation in python with example - đóng gói trong python với ví dụ

Đó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ướng dẫn encapsulation in python with example - đóng gói trong python với ví dụ

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

GeeksforGeeks
0self__1212
GeeksforGeeks
4

class

GeeksforGeeks
6

    def __init__(self

GeeksforGeeks
0
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' 
3self
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

GeeksforGeeks
0
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' 
9class0

class1selfclass3

GeeksforGeeks
0self__1212
GeeksforGeeks
4

GeeksforGeeks
0
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' 
8Base:2class0

class1selfclass3

class

GeeksforGeeks
6

GeeksforGeeks
0self__1212 class8

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    5    6

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    9def0

Output:  

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:7GeeksforGeeks3 Base:9

    0

GeeksforGeeks
3     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

GeeksforGeeks
0self__1212
GeeksforGeeks
4

class

GeeksforGeeks
6

class

GeeksforGeeks
6

    def __init__(self

GeeksforGeeks
0
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' 
3self
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

GeeksforGeeks
0
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):2
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

GeeksforGeeks
0
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' 
8self):8

GeeksforGeeks
0self__1212
GeeksforGeeks
4

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
GeeksforGeeks
03

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' 

Ví dụ đóng gói là gì?

Đóng gói trong Java là một quá trình gói mã và dữ liệu với nhau thành một đơn vị, ví dụ, một viên nang được trộn lẫn với một số loại thuốc. Chúng tôi có thể tạo một lớp được đóng gói đầy đủ trong Java bằng cách làm cho tất cả các thành viên dữ liệu của lớp riêng tư.a capsule which is mixed of several medicines. We can create a fully encapsulated class in Java by making all the data members of the class private.

Ví dụ tốt nhất về đóng gói là gì?

Túi trường là một trong những ví dụ thực sự nhất của đóng gói. Túi trường có thể giữ sách, bút, v.v ... Ví dụ về thời gian thực 2: Khi bạn đăng nhập vào tài khoản email của mình như Gmail, Yahoo Mail hoặc Rediff Mail, có rất nhiều quy trình nội bộ diễn ra trong phần phụ trợ và bạn không có quyền kiểm soát trên nó. is one of the most real examples of Encapsulation. School bag can keep our books, pens, etc. Realtime Example 2: When you log into your email accounts such as Gmail, Yahoo Mail, or Rediff mail, there is a lot of internal processes taking place in the backend and you have no control over it.

Đóng gói trong oops trong Python là gì?

Đóng gói là một trong những khái niệm cơ bản nhất trong lập trình hướng đối tượng (OOP).Đây là khái niệm gói dữ liệu và phương pháp hoạt động với dữ liệu trong một đơn vị.Điều này ngăn chặn sửa đổi dữ liệu vô tình bằng cách giới hạn quyền truy cập vào các biến và phương thức.the concept of wrapping data and methods that work with data in one unit. This prevents data modification accidentally by limiting access to variables and methods.

Ví dụ đóng gói và trừu tượng trong Python với ví dụ là gì?

Đóng gói có nghĩa là lưu trữ hoặc đặt dữ liệu ở một nơi duy nhất để làm cho nó dễ đọc và nhỏ gọn ở một nơi.Trong khi đó trừu tượng hóa dữ liệu trong lập trình Python có nghĩa là để ẩn các chức năng nội bộ đang thực hiện trên ứng dụng bằng cách sử dụng mã và chỉ hiển thị thông tin cần thiết (thuộc tính lớp).