<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                在Class內部,可以有屬性和方法,而外部代碼可以通過直接調用實例變量的方法來操作數據,這樣,就隱藏了內部的復雜邏輯。 但是,從前面Student類的定義來看,外部代碼還是可以自由地修改一個實例的`name`、`score`屬性: ~~~ >>> bart = Student('Bart Simpson', 98) >>> bart.score 98 >>> bart.score = 59 >>> bart.score 59 ~~~ 如果要讓內部屬性不被外部訪問,可以把屬性的名稱前加上兩個下劃線`__`,在Python中,實例的變量名如果以`__`開頭,就變成了一個私有變量(private),只有內部可以訪問,外部不能訪問,所以,我們把Student類改一改: ~~~ class Student(object): def __init__(self, name, score): self.__name = name self.__score = score def print_score(self): print('%s: %s' % (self.__name, self.__score)) ~~~ 改完后,對于外部代碼來說,沒什么變動,但是已經無法從外部訪問`實例變量.__name`和`實例變量.__score`了: ~~~ >>> bart = Student('Bart Simpson', 98) >>> bart.__name Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute '__name' ~~~ 這樣就確保了外部代碼不能隨意修改對象內部的狀態,這樣通過訪問限制的保護,代碼更加健壯。 但是如果外部代碼要獲取name和score怎么辦?可以給Student類增加`get_name`和`get_score`這樣的方法: ~~~ class Student(object): ... def get_name(self): return self.__name def get_score(self): return self.__score ~~~ 如果又要允許外部代碼修改score怎么辦?可以再給Student類增加`set_score`方法: ~~~ class Student(object): ... def set_score(self, score): self.__score = score ~~~ 你也許會問,原先那種直接通過`bart.score = 59`也可以修改啊,為什么要定義一個方法大費周折?因為在方法中,可以對參數做檢查,避免傳入無效的參數: ~~~ class Student(object): ... def set_score(self, score): if 0 <= score <= 100: self.__score = score else: raise ValueError('bad score') ~~~ 需要注意的是,在Python中,變量名類似`__xxx__`的,也就是以雙下劃線開頭,并且以雙下劃線結尾的,是特殊變量,特殊變量是可以直接訪問的,不是private變量,所以,不能用`__name__`、`__score__`這樣的變量名。 有些時候,你會看到以一個下劃線開頭的實例變量名,比如`_name`,這樣的實例變量外部是可以訪問的,但是,按照約定俗成的規定,當你看到這樣的變量時,意思就是,“雖然我可以被訪問,但是,請把我視為私有變量,不要隨意訪問”。 雙下劃線開頭的實例變量是不是一定不能從外部訪問呢?其實也不是。不能直接訪問`__name`是因為Python解釋器對外把`__name`變量改成了`_Student__name`,所以,仍然可以通過`_Student__name`來訪問`__name`變量: ~~~ >>> bart._Student__name 'Bart Simpson' ~~~ 但是強烈建議你不要這么干,因為不同版本的Python解釋器可能會把`__name`改成不同的變量名。 總的來說就是,Python本身沒有任何機制阻止你干壞事,一切全靠自覺。 ### 參考源碼 [protected_student.py](https://github.com/michaelliao/learn-python3/blob/master/samples/oop_basic/protected_student.py)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看