Python中的访问修饰符:公有(Public)、私有(Private)和受保护(Protected)

PHPz
发布: 2023-09-02 19:17:06
转载
1576 人浏览过

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 an OOPs principle 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 an example of Public access modifier −

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 are declared by adding a prefix with two underscores(‘__’) 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 are private so while accessing them from outside the class it raises Attribute 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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!