zoukankan      html  css  js  c++  java
  • Python私有变量(Private Variable)

    Variables can be private which can be useful on many occasions. A private variable can only be changed within a class method and not outside of the class.

    Objects can hold crucial data for your application and you do not want that data to be changeable from anywhere in the code.

    class Car:
     
        __maxspeed = 0
        __name = ""
     
        def __init__(self):
            self.__maxspeed = 200
            self.__name = "Supercar"
     
        def drive(self):
            print 'driving. maxspeed ' + str(self.__maxspeed)
     
    redcar = Car()
    redcar.drive()
    redcar.__maxspeed = 10  # will not change variable because its private
    redcar.drive()
    

    If you want to change the value of a private variable, a setter method is used. This is simply a method that sets the value of a private variable.

  • 相关阅读:
    Day2-Python爬虫小练 爬取百科词条
    Day1-python轻量级爬虫
    大数据处理课堂测试1
    周记7-28
    周记7-21
    周记7-14
    软件工程课程总结
    进度15
    NABCD
    团队项目成员和题目
  • 原文地址:https://www.cnblogs.com/yaos/p/14014323.html
Copyright © 2011-2022 走看看