<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Python 對象和類 > 原文: [https://thepythonguru.com/python-object-and-classes/](https://thepythonguru.com/python-object-and-classes/) * * * 于 2020 年 1 月 7 日更新 * * * ## 創建對象和類 * * * Python 是一種面向對象的語言。 在 python 中,所有東西都是對象,即`int`,`str`,`bool`甚至模塊,函數也是對象。 面向對象的編程使用對象來創建程序,這些對象存儲數據和行為。 ## 定義類 * * * python 中的類名以`class`關鍵字開頭,后跟冒號(`:`)。 類通常包含用于存儲數據的數據字段和用于定義行為的方法。 python 中的每個類還包含一個稱為*初始化器*的特殊方法(也稱為構造器),該方法在每次創建新對象時自動被調用。 讓我們來看一個例子。 ```py class Person: ? ? ? ?# constructor or initializer ? ? ? def __init__(self, name): ? ? ? ? ? ? self.name = name # name is data field also commonly known as instance variables ? ? ? # method which returns a string ? ? ?def whoami(self): ? ? ? ? ? ?return "You are " + self.name ``` 在這里,我們創建了一個名為`Person`的類,其中包含一個名為`name`和方法`whoami()`的數據字段。 ## 什么是`self`? * * * python 中的所有方法(包括一些特殊方法,如初始化器)都具有第一個參數`self`。 此參數引用調用該方法的對象。 創建新對象時,會自動將`__init__`方法中的`self`參數設置為引用剛創建的對象。 ## 從類創建對象 * * * ```py p1 = Person('tom') # now we have created a new person object p1 print(p1.whoami()) print(p1.name) ``` **預期輸出**: ```py You are tom tom ``` **注意**: 當您調用一個方法時,您無需將任何內容傳遞給`self`參數,python 就會在后臺自動為您完成此操作。 您也可以更改`name`數據字段。 ```py p1.name = 'jerry' print(p1.name) ``` **預期輸出**: ```py jerry ``` 盡管在類之外授予對您的數據字段的訪問權是一種不好的做法。 接下來,我們將討論如何防止這種情況。 ## 隱藏數據字段 * * * 要隱藏數據字段,您需要定義私有數據字段。 在 python 中,您可以使用兩個前劃線來創建私有數據字段。 您還可以使用兩個下劃線定義私有方法。 讓我們看一個例子 ```py class BankAccount: ? ? ?# constructor or initializer ? ? def __init__(self, name, money): ? ? ? ? ?self.__name = name ? ? ? ? ?self.__balance = money ? # __balance is private now, so it is only accessible inside the class ? ? def deposit(self, money): ? ? ? ? ?self.__balance += money ? ? def withdraw(self, money): ? ? ? ? ?if self.__balance > money : ? ? ? ? ? ? ?self.__balance -= money ? ? ? ? ? ? ?return money ? ? ? ? ?else: ? ? ? ? ? ? ?return "Insufficient funds" ? ? def checkbalance(self): ? ? ? ? ?return self.__balance b1 = BankAccount('tim', 400) print(b1.withdraw(500)) b1.deposit(500) print(b1.checkbalance()) print(b1.withdraw(800)) print(b1.checkbalance()) ``` **預期輸出**: ```py Insufficient funds 900 800 100 ``` 讓我們嘗試訪問類外部的`__balance`數據字段。 ```py print(b1.__balance) ``` **預期輸出**: ```py AttributeError: 'BankAccount' object has no attribute '__balance' ``` 如您所見,現在在類外部無法訪問`__balance`字段。 在下一章中,我們將學習[運算符重載](/python-operator-overloading/)。 * * * * * *
                  <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>

                              哎呀哎呀视频在线观看