python3-类成员变量的私有和公开

'''
成员的可见性:公开和私有
'''

class MyCat():
    def __init__(self,name):
        self.name = name
    
    def marking(self,score):
        if score < 0:
            return '不能给猫打负分'
        # 私有变量
        self.__score = score
        print(self.name +'本次的考评为:'+str(self.__score))

    # 私有方法在方法前加__
    def __marking1(self,score):
        if score < 0:
            return '不能给猫打负分'
        self.score = score
        print(self.name +'本次的考评为:'+str(self.score))

cat = MyCat('MIMI')
cat.marking(100)
print(cat.marking(-1))

你可能感兴趣的:(python3)