首頁 > 後端開發 > Python教學 > Python中的存取修飾符:公有(Public)、私有(Private)和受保護(Protected)

Python中的存取修飾符:公有(Public)、私有(Private)和受保護(Protected)

PHPz
發布: 2023-09-02 19:17:06
轉載
1615 人瀏覽過

Python中的存取修飾符:公有(Public)、私有(Private)和受保護(Protected)

Access modifiers are used by object oriented programming languages like C ,java,python etc. to restrict the access of the class member variable and methods from outside the class. Encapsulation is class member variable and methods from outside the class. En膠囊which protects the internal data of the class using Access modifiers like Public,Private and Protected.

Python支援三種存取修飾符,分別是public(公有)、private(私有)和protected(受保護)。這些存取修飾符對於從類別外部的任何物件存取類別的成員變數和方法提供了限制。

Public Access Modifier

By default the member variables and methods are public which means they can be accessed from anywhere outside or inside the class. No public keyword is required to make the class or methods and properties public.Here is required to make the class or methods

Example

的中文翻譯為:

範例

The student class has two member variables, name and age and a method display which prints the member variable values.Both these variables and the methods are public as no specific keyword is assigned to them.##

class Student:
   def __init__(self, name, age):
      self.name = name
      self.age = age
    
   def display(self):
      print("Name:", self.name)
      print("Age:", self.age)

s = Student("John", 20)
s.display() 
登入後複製

Output

#
Name: John
Age: 20
登入後複製

私有存取修飾符

Class properties and methods with private access modifier can only be accessed within the class where they are defined and cannot be accessed outside the class. In Python private properties and methods accessed by gem​​nsting aunderion' before their declaration.

Example

的中文翻譯為:

範例

The Class BankAccount is being declared with two private variables i.e account_number and balance and a private property display_balance which prints the balance of the bank account. As both the properties and method private classvate class private 完成 片面 片面骨error.

class BankAccount:
   def __init__(self, account_number, balance):
      self.__account_number = account_number
      self.__balance = balance
    
   def __display_balance(self):
      print("Balance:", self.__balance)

b = BankAccount(1234567890, 5000)
b.__display_balance() 
登入後複製

Output

#
AttributeError: 'BankAccount' object has no attribute '__display_balance'
登入後複製

Protected存取修飾符

具有受保護存取修飾符的類別屬性和方法可以在類別內部和繼承受保護類別的類別中存取。在Python中,受保護的成員和方法是在其名稱之前使用單一底線('_')作為前綴聲明的。

Example

的中文翻譯為:

範例

Person類別有兩個受保護的屬性,即_name和_age,還有一個受保護的方法_display,用來顯示Person類別的屬性值。 Student類別繼承自Person類,還有一個額外的受保護屬性_roll_number和一個公共方法display,該方法呼叫了父類Person類別的_display方法。透過建立Student類別的實例,我們可以從類別外呼叫display方法,因為display方法是私有的,它呼叫了Person類別的受保護的_display方法。

class Person:
   def __init__(self, name, age):
      self._name = name
      self._age = age
    
   def _display(self):
      print("Name:", self._name)
      print("Age:", self._age)

class Student(Person):
   def __init__(self, name, age, roll_number):
      super().__init__(name, age)
      self._roll_number = roll_number
    
   def display(self):
      self._display()
      print("Roll Number:", self._roll_number)

s = Student("John", 20, 123)
s.display() 
登入後複製

Output

#
Name: John
Age: 20
Roll Number: 123
登入後複製
結論

在本文中,我們了解了三種存取修飾符,這些修飾符用於Python和其他物件導向程式語言中的資料隱藏和保護。公用,私有和受保護是Python中使用的三種存取修飾符。類別的公共屬性和方法可以從類別的內部或外部的任何地方存取。私有成員變數和屬性只能從宣告這些屬性和方法的類別的內部存取。當我們需要從類別的內部以及從繼承自該類別的類別中存取類別的屬性和方法時,使用受保護的存取修飾符。

以上是Python中的存取修飾符:公有(Public)、私有(Private)和受保護(Protected)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板