类中变量私有化和调用:__x和getx/setx或者property
__xx:双前置下划线,子类不可继承属性、方法,父类私有。
详见:https://www.cnblogs.com/andy9468/p/8299448.html
例子1:隐藏数据:私有化后,用get和set方法
class MoneyClass(object):
def __init__(self):
self.__money = 0 def get_money(self):
return self.__money def set_money(self, value):
if isinstance(value, int):
self.__money = value
else:
print("error:不是整型数字") m1 = MoneyClass()
print(m1.get_money())
m1.set_money(50)
print(m1.get_money())
输出:
0
50
例子2:property属性:自动调用get、set方法
class MoneyClass(object):
def __init__(self):
self.__money = 0 def get_money(self):
return self.__money def set_money(self, value):
if isinstance(value, int):
self.__money = value
else:
print("error:不是整型数字") getsetmoney = property(get_money, set_money) m1 = MoneyClass()
print(m1.getsetmoney)
m1.getsetmoney = 800
print(m1.getsetmoney)
输出:
0
800
例子3:property装饰器:自动调用get、set方法
class MoneyClass(object):
def __init__(self):
self.__money = 0 @property
def getsetmoney(self):
return self.__money @getsetmoney.setter
def getsetmoney(self, value):
if isinstance(value, int):
self.__money = value
else:
print("error:不是整型数字") m1 = MoneyClass()
print(m1.getsetmoney)
m1.getsetmoney = 1000
print(m1.getsetmoney)
输出:
0
1000
类中变量私有化和调用:__x和getx/setx或者property的更多相关文章
- C#同一项目中不同文件或类中的方法进行调用
有两种方法,一是将被调用的类设置成静态类Static,这样就可以直接点出来了,二是将被调用的方法所在类设置成public,这几必须在调用类中先将被调用的类进行实体化,new()出来,再点出来. 一. ...
- 《同一个类中不同方法之间的调用相关问题(省略的类名或者this)》
//同一个类中不同方法之间的调用相关问题(省略的类名或者this) class A { public void B() { System.out.println("b方法运行"); ...
- 在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解失效的原因和解决方法
参考原贴地址:https://blog.csdn.net/clementad/article/details/47339519 在同一个类中,一个方法调用另外一个有注解(比如@Async,@Trans ...
- 【转】在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解失效的原因和解决方法
参考 原文链接 @Transactional does not work on method level 描述 在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational) ...
- Spring同一个类中的注解方法调用AOP失效问题总结
public interface XxxService { // a -> b void a(); void b(); } @Slf4j public class XxxServiceImpl ...
- 获取class对象的三种方法以及通过Class对象获取某个类中变量,方法,访问成员
public class ReflexAndClass { public static void main(String[] args) throws Exception { /** * 获取Clas ...
- 4. 在Inspector面板中显示类中变量+ 拓展编辑器
1. C#脚本如下: using UnityEngine; using System.Collections; public class MyTest : MonoBehaviour { ; ; [S ...
- 分析spring事务@Transactional注解在同一个类中的方法之间调用不生效的原因及解决方案
问题: 在Spring管理的项目中,方法A使用了Transactional注解,试图实现事务性.但当同一个class中的方法B调用方法A时,会发现方法A中的异常不再导致回滚,也即事务失效了. 当这个方 ...
- Python类中的装饰器在当前类中的声明与调用
[本文出自天外归云的博客园] 我的Python环境:3.7 在Python类里声明一个装饰器,并在这个类里调用这个装饰器.代码如下: class Test(): xx = False def __in ...
随机推荐
- 改进初学者的PID-积分饱和
最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...
- Swift4.0复习特性、编译标志和检查API的可用性
1.Swift中的特性: @引出,后面紧跟特性名,圆括号带参数即可. @attribute(args) avaiable: 指明对象,函数,类型的可用性. @available(iOS 10.0, m ...
- [转]gcc的__builtin_函数介绍
链接地址:https://blog.csdn.net/jasonchen_gbd/article/details/44948523
- web端自动化——selenium测试报告生成、找到测试报告路径、实现发邮件(整合)
有这样的一个场景: 假设生成的测试报告与多人相关,每个人都去测试服务器査看就会比较麻烦,如果把这种主动的且不及时的査看变成被动且及时的査收,就方便多了. 整个程序的执行过程可以分为三个步骤: ① ...
- Django 之上下文处理器和中间件
一.上下文处理器 上下文处理器是可以返回一些数据,在全局模板中都可以使用.比如登录后的用户信息,在很多页面中都需要使用,那么我们可以放在上下文处理器中,就没有必要在每个视图函数中都返回这个对象. 在s ...
- 安卓app和苹果app共用一个二维码
应项目要求,现在安卓app和苹果app共用一个二维码,对外提供下载: <html> <head> <meta http-equiv="Content-Type& ...
- 1、Tensorflow 之 saver与checkpoint
1.Tensorflow 模型文件 checkpoint model.ckpt-200.data-00000-of-00001 model.ckpt-200.index model.ckpt-200. ...
- 软件素材--c/c++干掉代码的通用方法
while(1) { sleep(200); } #endif
- SQLServer分页查询方法整理以及批量插入操作SqlBulkCopy
分页查询 通用方法:sqlserver 2005 + ROW_NUMBER() OVER()方式: ; TOP NOT IN方式 : ID FROM TripDetail ORDER BY ID) O ...
- linux--Linux 各目录及每个目录的详细介绍
2017年08月31日 17:53:38 worthsen 阅读数 3490更多 所属专栏: Linux 版权声明:本文为博主原创文章,如要转载,请注明地址,谢谢^...^ https://blo ...