美文网首页
类的私有变量

类的私有变量

作者: cc_cv | 来源:发表于2020-08-05 11:21 被阅读0次
class Person(object):
    public_var = "公有"                     # 类的静态变量,由类调用,子类可访问
    __private_var = "私有"                  # 类的私有变量,只能由类内部调用,类外部及子类不能访问
    def __init__(self, name, age):
        self.name = name                    # 实例的成员变量,由对象调用self.name,子类可访问
        self.__age = age                    # 实例的私有变量,只能由类内部调用,类外部及子类不能访问
    @classmethod
    def class_func(cls):                    # 类的方法,由类调用Person.class_func(),子类和子类的实例都可以访问
        print ("class method")
    @staticmethod
    def static_func():                      # 类的静态方法,由类调用Person.static_func(),子类和子类的实例都可以访问
        print ("static method")
    def comm_func(self):                    # 实例的方法,由对象调用self.comm_func()
        print ("{} is {} years old.".format(self.name, self.__age))
    def __private_func(self):               # 类的私有方法,只能由类内部调用,类外部及子类不能访问
        print ("The value of __private_var is {}.".format(self.__private_var))
    def proxy_private(self):
        self.__private_func()
    @property
    def age(self):                          # 属性封装,由对象调用self.age
        return self.__age

class Student(Person):
    pass

>>> s = Student("Tom", 18)
>>> s.public_var
'公有'
>>> s.__private_var                            # 类的私有变量,只能由类内部调用,类外部及子类不能访问
Traceback (most recent call last):
  File "<pyshell#89>", line 1, in <module>
    s.__private_var
AttributeError: 'Student' object has no attribute '__private_var'
>>> s.name
'Tom'
>>> s.__age                                # 实例的私有变量,只能由类内部调用,类外部及子类不能访问
Traceback (most recent call last):
  File "<pyshell#91>", line 1, in <module>
    s.__age
AttributeError: 'Student' object has no attribute '__age'
>>> s.comm_func()
Tom is 18 years old.
>>> s.age
18
>>> s.__private_func()                          # 类的私有方法,只能由类内部调用,类外部及子类不能访问
Traceback (most recent call last):
  File "<pyshell#94>", line 1, in <module>
    s.__private_func()
AttributeError: 'Student' object has no attribute '__private_func'
>>> s.proxy_private()
The value of __private_var is 私有.
>>> Student.class_func()
class method
>>> s.class_func()
class method
>>> Student.static_func()
static method
>>> s.static_func()
static method
>>>

相关文章

  • clean code读书笔记(三)

    一、类 类从变量开始,公共静态常量-->私有静态变量-->私有实体变量,公共函数应跟在变量列表之后。 类要短小,职...

  • 2018-06-28 python 类的私有变量和私有方法

    转发python 类的私有变量和私有方法 类的私有变量和私有方法在Python中可以通过在属性变量名前加上双下划线...

  • js的一些新特性

    一、类的私有变量最新提案之一是在类中添加私有变量的方法。我们将使用 # 符号表示类的私有变量。这样就不需要使用闭包...

  • 十、类

    1.类的组织 遵循标准java约定,类的组织最好是这样的顺序。公共静态常量->私有静态变量->公共变量->私有变量...

  • 1、类的定义 类的成员出现顺序:公共静态常量、私有静态变量、私有实体变量,然后才是公共函数,私有的工具函数紧随在该...

  • clean code笔记7:类

    类 类的组织 类应该从一组变量列表开始。如果有公共静态常量,应该先出现。然后是私有静态变量,以及私有实体变量。很少...

  • 第十章:代码简洁之道:类

    一、类的组织 遵循Java约定,类的组织顺序:公有静态常量、私有静态变量、私有实体变量。接下来是公共函数紧跟在变量...

  • C++学习第2课,笔记

    1 类 成员变量 成员函数 private://私有的 public://公有的 *1 公有函数修改私有变量; *...

  • 类的私有变量

  • objc_runtime

    获取某个类的成员变量; 获取成员函数 UISearchBar的私有变量

网友评论

      本文标题:类的私有变量

      本文链接:https://www.haomeiwen.com/subject/rghbrktx.html