0

0

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

PHPz

PHPz

发布时间:2023-09-02 19:17:06

|

3388人浏览过

|

来源于tutorialspoint

转载

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.

立即学习Python免费学习笔记(深入)”;

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速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

43

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

84

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

24

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

35

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

16

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

56

2026.01.15

浏览器缓存清理方法汇总
浏览器缓存清理方法汇总

本专题整合了浏览器缓存清理教程汇总,阅读专题下面的文章了解更多详细内容。

16

2026.01.15

ps图片相关教程汇总
ps图片相关教程汇总

本专题整合了ps图片设置相关教程合集,阅读专题下面的文章了解更多详细内容。

9

2026.01.15

ppt一键生成相关合集
ppt一键生成相关合集

本专题整合了ppt一键生成相关教程汇总,阅读专题下面的的文章了解更多详细内容。

26

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.3万人学习

Django 教程
Django 教程

共28课时 | 3.2万人学习

React 教程
React 教程

共58课时 | 3.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号