博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 学习笔记 -- 类的访问限制
阅读量:7240 次
发布时间:2019-06-29

本文共 719 字,大约阅读时间需要 2 分钟。

#在Python中其实并没有像C++一样的,private,public,protected这些关键字用来限制访问权限#但是我们可以在属性和方法的前面加上__双下划先class testClass(object):    def __init__(self):        self.__name = "欧米茄"        self.score = 100    def __print_score(self):        print(self.score)    def print_name(self):        print(self.__name)test = testClass()print("没有加双下划线的score:",test.score)try:    test.__nameexcept:    print("虽然看上去像是实现访问权限限制,但是其实没有!")    print("它的真实名字其实是test._testClass__name : ",test._testClass__name)print("没有加双下划线的print_name:")test.print_name()try:    test.__print_score()except:    print("同理,其实它只是改了个名字,真实名字test._testClass__print_score")    test._testClass__print_score()   #真正的名字其实就是_类名__XXXXXX

 

转载于:https://www.cnblogs.com/jiangchenxi/p/8053603.html

你可能感兴趣的文章
在cisco交换机上禁止某个特定MAC地址转发
查看>>
Exchange 2010之灾难恢复
查看>>
我的友情链接
查看>>
Mac commands
查看>>
关于清理distribution数据库过期数据
查看>>
VMware Horizon View 7: Create Events Database [Part 3]
查看>>
我的友情链接
查看>>
深入解析thinkphp中的addAll方法
查看>>
SSD上如何进行数据保护?
查看>>
Linux基础之文件系统简介及其系统管理工具
查看>>
我的友情链接
查看>>
PutText 在图像上显示文字
查看>>
我的友情链接
查看>>
Juniper T320 Control Board 0更换步骤
查看>>
我的友情链接
查看>>
git安装
查看>>
sphinx 全文搜索应用(二)
查看>>
mysql存储引擎MyISAM与InnoDB的优劣
查看>>
集群与负载均衡技术的区别
查看>>
Android开发工具
查看>>