__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的更多相关文章

  1. C#同一项目中不同文件或类中的方法进行调用

    有两种方法,一是将被调用的类设置成静态类Static,这样就可以直接点出来了,二是将被调用的方法所在类设置成public,这几必须在调用类中先将被调用的类进行实体化,new()出来,再点出来. 一. ...

  2. 《同一个类中不同方法之间的调用相关问题(省略的类名或者this)》

    //同一个类中不同方法之间的调用相关问题(省略的类名或者this) class A { public void B() { System.out.println("b方法运行"); ...

  3. 在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解失效的原因和解决方法

    参考原贴地址:https://blog.csdn.net/clementad/article/details/47339519 在同一个类中,一个方法调用另外一个有注解(比如@Async,@Trans ...

  4. 【转】在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解失效的原因和解决方法

    参考 原文链接 @Transactional does not work on method level 描述 在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational) ...

  5. Spring同一个类中的注解方法调用AOP失效问题总结

    public interface XxxService { // a -> b void a(); void b(); } @Slf4j public class XxxServiceImpl ...

  6. 获取class对象的三种方法以及通过Class对象获取某个类中变量,方法,访问成员

    public class ReflexAndClass { public static void main(String[] args) throws Exception { /** * 获取Clas ...

  7. 4. 在Inspector面板中显示类中变量+ 拓展编辑器

    1. C#脚本如下: using UnityEngine; using System.Collections; public class MyTest : MonoBehaviour { ; ; [S ...

  8. 分析spring事务@Transactional注解在同一个类中的方法之间调用不生效的原因及解决方案

    问题: 在Spring管理的项目中,方法A使用了Transactional注解,试图实现事务性.但当同一个class中的方法B调用方法A时,会发现方法A中的异常不再导致回滚,也即事务失效了. 当这个方 ...

  9. Python类中的装饰器在当前类中的声明与调用

    [本文出自天外归云的博客园] 我的Python环境:3.7 在Python类里声明一个装饰器,并在这个类里调用这个装饰器.代码如下: class Test(): xx = False def __in ...

随机推荐

  1. C++接口的概念

    满足下面条件: 1.类中没有定义任何的成员变量 2.所有的成员函数都是公有的 3.所有的成员函数都是纯虚函数 4.接口是一种特殊的抽象类

  2. 【swoole】PHP+Swoole+Linux实现进程监控

    脚本代码 class Server { const PORT = 8888; public function port() { //netstat -anp 2>/dev/null| grep ...

  3. plink修改正负链(--flip, change the positive and negative stand)

    修改正负链用到的参数为--flip 假定trial.bim的内容如下: trial.bim 1 rs142578063 0 732746 G A 1 rs144022023 0 732801 G A ...

  4. Delphi XE6 使用定时器或者线程解决程序界面无响应问题

    ---恢复内容开始--- 介绍 在手机应用上,我们不应该使用速度慢的代码,当然我们在桌面程序上也应该避免这个,当手机应用长时间没有相应的时候,程序会提示“程序没响应,是否关闭”的提示,这个非常不好,所 ...

  5. 查看Mysql是否开启binlog

    show variables like 'log_bin';

  6. 1、Tensorflow 之 saver与checkpoint

    1.Tensorflow 模型文件 checkpoint model.ckpt-200.data-00000-of-00001 model.ckpt-200.index model.ckpt-200. ...

  7. Spring bean加载之1:BeanFactory和FactoryBean

    BeanFactory BeanFactory:以Factory结尾,表示它是一个工厂类(接口),用于管理Bean的一个工厂.在Spring中,BeanFactory是IOC容器的核心接口,它的职责包 ...

  8. TypeScript 高级类型 类(class)

    传统的JavaScript程序使用函数和基于原型的继承来创建可重用的组件,但对于熟悉使用面向对象方式的程序员来讲就有些棘手,因为他们用的是基于类的继承并且对象是由类构建出来的. 从ECMAScript ...

  9. SSM整合-配置文件

    使用工具:maven.idea.jdk8.mysql.tomcat9.0 初学ssm框架,配置文件的配置目录:                                     其中genera ...

  10. Jmeter-后置处理器--json提取器

    Token提取: 将token放入全局变量: 将token值设为全局变量,${__setProperty(newtoken,${token},)}  添加请求头部管理器作为全局使用,将变量token使 ...