python 类属性、私有属性

class Emp:
    clspro=0         #类属性
    _clspro1=1       # 单下划线  无限制
   __clspro2=2      # 双下划线 不能在类的外部 使用 和 调用(内部使用可以用  self. 去掉用)
   __clspro3__=3    # 内建方法,用户不要这样定义
    def __init__(self,name):
self.name=name #实例属性 同类属性
        self._name=name 
        self.__name=name
        self.__name1__=name

   #方法同样()
@classmethod
def one(cls):
    print(cls.__clspro2)

# @staticmethod (静态方法/只能用类名。去调用)
def two(self):#(普通方法)
    print(self._name)
 a=Emp( "ss") # print(a.clspro) #0
# print(a._clspro1) #1
# print(a.__clspro2) # AttributeError: 'Emp' object has no attribute '__clspro2'
# print(a.__clspro3__) #3

# print(Emp.clspro) #0
# print(Emp._clspro1) #1
# print(Emp.__clspro2) #AttributeError: type object 'Emp' has no attribute '__clspro2'
# print(Emp.__clspro3__) #3
# print(a.name)#ss
# print(a._name)#ss
# print(a.__name) #AttributeError: 'Emp' object has no attribute '__name'
# print(a.__name1__) #ss

# print(Emp.name) AttributeError: type object 'Emp' has no attribute 'name'

# print(Emp._name) type object 'Emp' has no attribute '_name'

# print(Emp.__name) type object 'Emp' has no attribute '_name'

# print(Emp.__name1__) type object 'Emp' has no attribute '__name1__'

出入茅庐,希望大家多多指点

猜你喜欢

转载自blog.csdn.net/xiaoleizhanghahaha/article/details/79414503
今日推荐