python_公用、受保护、私有属性及方法

一。列举:

def test_private_vs_protected_vs_public():
"""
public: 共有的
private:私有的
protected:受保护的
:return:
"""
pass
#1.public
x=1
#2.protected,在导入 所有也就是 import * 时不可调用,如果导入 import _y就可以使用
_y=2


#3.private
class Li:
def __init__(self):
"""
私有属性
"""
self.__j = 1

def __fi(self):
"""
私有方法
:return:
"""
print("私有方法")

a = Li()


if __name__ == '__main__':
#私有属性不能被别人调用
print(a.__j)
print(a.__fi)


二。受保护变量/方法调用
# from 路径 import *
from 路径 import _y, x

print(x)
print(_y)
爱折腾的小测试
原文地址:https://www.cnblogs.com/newsss/p/14583861.html