zoukankan      html  css  js  c++  java
  • python:(类)私有

    私有:某个类中,在变量或方法前面加__,就变成私有变量或私有方法。只能在这个类中使用

    class Person():
        def __init__(self):
            self.__money=5000   #在变量或函数前加__,就是私有变量。只能在类中使用。
        def __kiss(self):   #只能在类中使用
            print('kiss')
        def work(self):
            self.__money+=2000               #调用私有变量,也要加__
            self.balance()
        def shoulaji(self):
            self.__money+=5000          #调用私有变量
            self.balance()
            self.__kiss()   #调用私有方法
        def buy(self,consume):
            self.__money-=consume
            self.balance()
        def __beat(self):
            print('打他')
        def balance(self):  #在类中的方法不按顺序执行,是在实例化后,在类中找对应的方法
            print('现在还有多少钱%s'%self.__money)   #私有变量调用时也时self.__money
    syy=Person()
    # syy.__money-=10000    #类外部调用私有变量会报错
    syy.shoulaji()
    syy.balance()
    syy.buy(200)
    # syy.__kiss()   #私有变量、函数不能在外边直接调用
  • 相关阅读:
    Use JAWS 14 in a VM
    远程文件拷贝
    MVA Prototype Only User License
    Global.asax 详解
    GridView of different size items
    Windows Phone 8, 添加Map控件
    动态规划——递归写法和递推写法
    守形数
    遍历链表
    成绩排序
  • 原文地址:https://www.cnblogs.com/hancece/p/11158482.html
Copyright © 2011-2022 走看看