别院牧志知识库 别院牧志知识库
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 辨析

  • Sockets编程

  • Django

  • stackoverflow

  • Flask

  • 全栈之路

    • 基础篇

    • 进阶篇

    • 面向对象

      • Python 全栈之路系列之面向对象类成员特性
      • Python 全栈之路系列之面向对象成员修饰符
        • 通过特殊的方法去访问类中的私有成员
      • Python 全栈之路系列之面向对象运算符重载
      • Python 全栈之路系列之面向对象进阶及类成员
      • Python 全栈之路系列之面向对象基础
      • Python 全栈之路系列之面向对象特殊成员
    • 网络编程

    • 操作系统

    • 标准库

    • 第三方库

    • Python 全栈之路
  • 面试

  • 代码片段

  • 异步编程

  • 😎Awesome资源

  • PEP

  • Python工匠系列

  • 高阶知识点

  • Python 学习资源待整理
  • 设计模式

  • 好“艹蛋”的 Python 呀!
  • FIFO | 待学清单📝
  • pip 安装及使用
  • 数据分析

  • 源码阅读计划

  • OOP

  • 关于 python 中的 setup.py
  • 并行分布式框架 Celery
  • 七种武器,让你的代码提高可维护性
  • 使用 pdb 调试 Python 代码
  • 每周一个 Python 标准库
  • 🐍Python
  • 全栈之路
  • 面向对象
佚名
2020-05-23
目录

Python 全栈之路系列之面向对象成员修饰符

成员修饰符就是设置类的成员有些是公开的有些是私有的,公开的是在外部通过对象或者类可以调用,但是私有的只能通过类的内部才可以调用。

  • 静态字段修饰
#!/usr/bin/env python
# _*_coding:utf-8 _*_

class Foo:
    # 公有的静态字段
    ClassMembers = "公开的"
    # 私有的静态字段
    __ClassMembers = "私有的"

# 执行公有的静态字段
print(Foo.ClassMembers)

# 执行私有的静态字段
print(Foo.__ClassMembers)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/usr/bin/python3.5 /home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py
公开的
Traceback (most recent call last):
  File "/home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py", line 14, in <module>
    print(Foo.__ClassMembers)
AttributeError: type object 'Foo' has no attribute '__ClassMembers'

Process finished with exit code 1
1
2
3
4
5
6
7
8

私有的是不能够直接调用的,需要在类中进行调用,如下:

#!/usr/bin/env python
# _*_coding:utf-8 _*_

class Foo:

    # 私有的静态字段
    __ClassMembers = "私有的"

    # 通过类中的方法调用私有的静态字段进行输出
    def Members(self):
        print(Foo.__ClassMembers)

# 创建一个对象
obj = Foo()

# 执行类中的Members方法
obj.Members()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

执行结果

/usr/bin/python3.5 /home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py
私有的

Process finished with exit code 0
1
2
3
4

普通字段修饰

#!/usr/bin/env python
# _*_coding:utf-8 _*_

class Foo:

    # 类的构造方法
    def __init__(self, url):

        # 普通字段
        self.url = url

        # 私有普通字段
        self.__Blog = url

# 创建一个对象,传入一个值
obj = Foo("http://ansheng.me")

# 普通字段
print(obj.url)

# 私有的普通字段
print(obj.__Blog)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

输出

/usr/bin/python3.5 /home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py
http://ansheng.me
Traceback (most recent call last):
  File "/home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py", line 22, in <module>
    print(obj.__Blog)
AttributeError: 'Foo' object has no attribute '__Blog'

Process finished with exit code 1
1
2
3
4
5
6
7
8

若要输出私有的普通字段,需要在类中调用私有的普通字段进行输出

#!/usr/bin/env python
# _*_coding:utf-8 _*_

class Foo:

    # 类的构造方法
    def __init__(self, url):

        # 私有普通字段
        self.__Blog = url

        # 直接在狗仔方法没输出传入的URL
        print(self.__Blog)

# 创建一个对象,传入一个值
obj = Foo("http://ansheng.me")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

输出结果

ansheng@Darker:~$ python3 /home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py
http://ansheng.me
1
2

对于私有的成员,只能够在类中进行访问,即使是继承关系也不可以,测试如下:

#!/usr/bin/env python
# _*_coding:utf-8 _*_

class Foo:

    # 父类的构造方法
    def __init__(self):

        # 私有普通字段
        self.__Blog = "http://ansheng.me"

# Bar继承了Foo类,
class Bar(Foo):

    # 由于Bar类没有构造方法,所以会执行他父类的构造方法

    # 创建一个类方法fetch
    def fetch(self):
        # 输出self.__Blog
        print(self.__Blog)

# 创建一个对象
obj = Bar()

# 执行类中的fetch方法
obj.fetch()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

输出

ansheng@Darker:~$ python3 /home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py
Traceback (most recent call last):
  File "/home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py", line 26, in <module>
    obj.fetch()
  File "/home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py", line 20, in fetch
    print(self.__Blog)
AttributeError: 'Bar' object has no attribute '_Bar__Blog'
1
2
3
4
5
6
7

对于普通方法、静态方法类方法也是如此,只要成员前面加两个下划线就代表是私有的,即外部不能够访问,只有内部才可以访问。

# 通过特殊的方法去访问类中的私有成员

#!/usr/bin/env python
# _*_coding:utf-8 _*_

class Foo:

    # 父类的构造方法
    def __init__(self):

        # 私有普通字段
        self.__Blog = "http://ansheng.me"

# 创建一个对象
obj = Foo()

# 通过特殊的方法访问
print(obj._Foo__Blog)
# 一个下划线,一个类名,私有的变量名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

输出

ansheng@Darker:~$ python3 /home/ansheng/文档/Python_code/sublime/Week06/Day04/s2.py
http://ansheng.me
1
2
编辑 (opens new window)
#编码
上次更新: 2024-07-23, 01:00:43
Python 全栈之路系列之面向对象类成员特性
Python 全栈之路系列之面向对象运算符重载

← Python 全栈之路系列之面向对象类成员特性 Python 全栈之路系列之面向对象运算符重载→

最近更新
01
2025 面试记录
05-28
02
提升沟通亲和力的实用策略
03-26
03
工作
07-15
更多文章>
Theme by Vdoing | Copyright © 2019-2025 IMOYAO | 别院牧志
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式