Python私有變數

FrankYou發表於2016-08-25
# 訪問限制的保護,程式碼更加健壯
class Student(object):
    def __init__(self,name,score):
        self.__name= name
        self.__score = score
    
    def print_scrore(self):
        print("%s:%s" % (self.__name,self.__score))
    
    def get_name(self):
        return self.__name
    
    def get_score(self):
        return self.__score

    def set_score(self):
        if 0<=score<=100:
            self.__score=score
        else:
            raise ValueError('bad score')

bart = Student('frank',100)
print(bart.get_name())
print(bart.get_score())

 

相關文章