Hướng dẫn how is encapsulation done in python? - đóng gói được thực hiện như thế nào trong python?

Đó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

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

Đóng gói trong OOP 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.

Làm thế nào là đóng gói đạt được?

Trong đóng gói, các biến của một lớp được ẩn khỏi các lớp khác và chỉ có thể được truy cập bằng các phương pháp của lớp mà chúng được tìm thấy.Đóng gói trong Java là một quy trình hướng đối tượng để kết hợp các thành viên dữ liệu và phương thức dữ liệu của lớp bên trong lớp do người dùng xác định.combining the data members and data methods of the class inside the user-defined class.

Python có cho phép đóng gói không?

Python không có từ khóa riêng, không giống như một số ngôn ngữ định hướng đối tượng khác, nhưng có thể đóng gói.Thay vào đó, nó dựa vào quy ước: một biến lớp không nên truy cập trực tiếp nên được đặt trước với một dấu gạch dưới.encapsulation can be done. Instead, it relies on the convention: a class variable that should not directly be accessed should be prefixed with an underscore.

Các loại đóng gói trong Python là gì?

Có ba loại đóng gói ...
Đóng gói biến thành viên ..
Đóng gói chức năng ..
Đóng gói lớp ..

Bài Viết Liên Quan

Chủ Đề