Làm cách nào để gọi một phương thức riêng tư trong PHP?

Đó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] trong Python. 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 ra các hạn chế đối với việc truy cập trực tiếp các biến và phương thức và có thể ngăn việc sửa đổi dữ liệu ngẫu nhiên. Một lớp là một ví dụ về đóng gói vì nó đóng gói tất cả dữ liệu là các hàm thành viên, biến, v.v. Bây giờ, có thể có một số tình huống trong đó chúng ta cần đặt các hạn chế đối với một số phương thức của lớp để chúng không thể được truy cập bên ngoài lớp cũng như bởi bất kỳ lớp con nào. Để thực hiện phương pháp riêng này đi vào chơi

P hàm riêng trong Python

Hãy xem xét một ví dụ thực tế, một động cơ ô tô, được tạo thành từ nhiều bộ phận như bugi, van, pít-tông, v.v. Không người dùng nào trực tiếp sử dụng các bộ phận này, thay vào đó họ chỉ biết cách sử dụng các bộ phận sử dụng chúng. Đây là những phương pháp riêng tư được sử dụng cho. Nó được sử dụng để ẩn chức năng bên trong của bất kỳ lớp nào với thế giới bên ngoài. Các phương thức riêng tư là những phương thức không đượ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 tồn tại các phương thức Riêng tư không thể truy cập ngoại trừ bên trong một lớp. Tuy nhiên, để xác định một phương thức riêng, tiền tố tên thành viên có dấu gạch dưới kép “__”. Ghi chú. 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.  

Python3




# Creating a Base class

class Base:

 

    # Declaring public method

    

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
0
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
1
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
2
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
3

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______0_______5_______0_______6
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
7_______0_______8

 

    

Public method
Private method
0

    

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
0
Public method
Private method
3
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
2
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
3

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______0_______5_______0_______6
Public method
Private method
9
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
8

 

Private method
1

 

 

class

Private method
3

    

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
0
Private method
6
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
2
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
3

 

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______85_______0

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4# Creating a Base class2

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______85_______4
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
2
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
8

 

    

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
0 # Creating a Base class9
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
2
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
3

 

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4class3

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______0_______5_______0_______6class7_______0_______8

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______0_______2Base:1

 

    

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
0 Base:4
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
2
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
3

 

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______87_______8

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______0_______2    1

 

 

    2

    3____88_______4     5

 

    6

    7

 

    8_______88_______4 # Declaring public method0

# Declaring public method1

 

# Declaring public method2

# Declaring public method3

 

# Declaring public method4

# Declaring public method5

đầu ra

Public method

Inside derived class
Public method
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'

Ví dụ trên cho thấy rằng các phương thức private của lớp không thể được truy cập bên ngoài lớp cũng như bởi bất kỳ lớp cơ sở nào. Tuy nhiên, các phương thức riêng tư có thể được truy cập bằng cách gọi các phương thức riêng tư thông qua các phương thức công khai.  

Thí dụ.  

Python3




# Declaring public method6

class # Declaring public method8

 

    # Declaring public method

    

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
0
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
1
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
2
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
3

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______0_______5_______0_______6
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
7_______0_______8

 

    

Public method
Private method
0

    

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
0
Public method
Private method
3
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
2
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
3

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______0_______5_______0_______6
Public method
Private method
9
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
8

 

    

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
14

    

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
16

    

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
0
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
19
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
6
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
2
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
3

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______0_______2Base:1

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
4_______0_______2    1

 

 

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
29

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
30_______88_______4
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
32

Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
33
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
19
Traceback [most recent call last]:
  File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in 
    obj1.__fun[] 
AttributeError: 'Base' object has no attribute '__fun'

Traceback [most recent call last]:
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in 
    obj2.call_private[] 
  File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
    self.__fun[]
AttributeError: 'Derived' object has no attribute '_Derived__fun'
35

đầu ra

Public method
Private method

xáo trộn tên

Python cung cấp một cây đũa thần có thể được sử dụng để gọi các phương thức riêng tư bên ngoài lớp, nó được gọi là xáo trộn tên. Điều đó có nghĩa là bất kỳ mã định danh nào có dạng __geek [ít nhất hai dấu gạch dưới ở đầu hoặc nhiều nhất là một dấu gạch dưới ở cuối] được thay thế bằng _classname__geek, trong đó tên lớp là tên lớp hiện tại với [các] dấu gạch dưới ở đầu bị tước.  

Làm cách nào để truy cập các phương thức riêng tư trong php?

Từ khóa riêng tư là công cụ sửa đổi quyền truy cập. Nó đánh dấu một thuộc tính hoặc phương thức là riêng tư. Các thuộc tính và phương thức riêng chỉ có thể được sử dụng bởi lớp trong đó thuộc tính hoặc phương thức được định nghĩa. Các lớp dẫn xuất và mã bên ngoài không thể sử dụng chúng.

Làm cách nào để gọi một phương thức riêng bên ngoài lớp trong php?

Nếu hai đối tượng cùng loại thì một đối tượng có thể gọi phương thức riêng của đối tượng khác. cú pháp. Bất kỳ biến, thuộc tính hoặc phương thức nào cũng có thể được khai báo là riêng tư bằng cách thêm tiền tố vào từ khóa “riêng tư” .

Cách chính xác để gọi một phương thức trong php là gì?

Làm thế nào để gọi một phương thức? .
Đầu tiên, chúng ta tạo một đối tượng [ $example ] từ lớp Ví dụ
Tiếp theo, chúng ta gọi phương thức echo với -> [toán tử đối tượng] và [] [dấu ngoặc đơn]
Các dấu ngoặc đơn chứa các đối số như bình thường

Làm cách nào để sử dụng biến riêng trong php?

Cách tốt nhất để khai báo một biến riêng trong Lớp PHP là tạo chúng phía trên phương thức __Construction , theo quy ước, bạn có thể bắt đầu . e $_private_variable] để cho các lập trình viên khác đang đọc mã của bạn biết ngay rằng đó là một biến riêng tư, tuyệt vời.

Chủ Đề