Python类的访问权限(public、 private、protected)
- 使用一个下画线“_”开头的属性或方法为保护(protected)属性或方法,只能在类或其派生类中访问,在类内部以“self._属性名或方法名”的方式使用;
- 其他的属性或方法为公有(public)属性或方法,可在类的外部直接访问,在类内部以“self.属性名或方法名”的方式使用。
以下例子展示了三种不同访问权限的属性和方法:
class Class1: public1= 111 _protected1 = 222 _private1 = 333 def publicFunc1(self): pass def _protectedFunc1(self): pass def __privateFunc1(self): pass class Class2(Class1): public2 = 444 _protected2 = 555 __private2 = 666 def publicFunc2(self): pass def _protectedFunc2(self): pass def __privateFunc2(self): pass c1 = Class1() print(c1.public1) print(c1._protected1) print(c1.__private1) c1.publicFunc1() c1._protectedFunc1() c1.__privateFunc1() c2 = Class2() print(c2.public1) print(c2._protected1) print(c2.__private1) print (c2.public2) print(c2._protected2) print(c2.__private2) c2.publicFunc1() c2._protectedFunc1() c2.__privateFunc1() c2.publicFunc2() c2._protectedFunc2() c2.__privateFunc2()上述代码的运行结果如下所示:
>>> class Class1:
... public1= 111
... protected1 = 222
... _private1 = 333
... def publicFunc1(self):
... pass
... def _protectedFunc1(self):
... pass
... def __privateFunc1(self):
... pass
>>> class Class2(Class1):
... public2 = 444
... _protected2 = 555
... __private2 = 666
... def publicFunc2(self):
... pass
... def _protectedFunc2(self):
... pass
... def __privateFunc2(self):
... pass
>>> c1 = Class1()
>>> print(c1.public1)
111
>>> print(c1._protected1)
222
>>> print(c1.__private1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
print(c1.__private1)
AttributeError: 'Class1' object has no attribute '__private1'
>>> c2 = Class2()
>>> print(c2.public1)
111
>>> print(c2._protected1)
222
>>> print(c2.__private1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
print(c2.__private1)
AttributeError: 'Class2' object has no attribute '__private1'
>>> print (c2.public2)
444
>>> print(c2._protected2)
555
>>> print(c2.__private2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
print(c2.__private2)
AttributeError: 'Class2' object has no attribute '__private2'
>>> c2.publicFunc1()
>>> c2._protectedFunc1()
>>> c2.__privateFunc1()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
c2.__privateFunc1()
AttributeError: 'Class2' object has no attribute '__privateFunc1'
>>> c2.publicFunc2()
>>> c2._protectedFunc2()
>>> c2.__privateFunc2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
c2.__privateFunc2()
AttributeError: 'Class2' object has no attribute '__privateFunc2'
所有教程
- C语言入门
- C语言编译器
- C语言项目案例
- 数据结构
- C++
- STL
- C++11
- socket
- GCC
- GDB
- Makefile
- OpenCV
- Qt教程
- Unity 3D
- UE4
- 游戏引擎
- Python
- Python并发编程
- TensorFlow
- Django
- NumPy
- Linux
- Shell
- Java教程
- 设计模式
- Java Swing
- Servlet
- JSP教程
- Struts2
- Maven
- Spring
- Spring MVC
- Spring Boot
- Spring Cloud
- Hibernate
- Mybatis
- MySQL教程
- MySQL函数
- NoSQL
- Redis
- MongoDB
- HBase
- Go语言
- C#
- MATLAB
- JavaScript
- Bootstrap
- HTML
- CSS教程
- PHP
- 汇编语言
- TCP/IP
- vi命令
- Android教程
- 区块链
- Docker
- 大数据
- 云计算