<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://www.programiz.com/python-programming/object-oriented-programming](https://www.programiz.com/python-programming/object-oriented-programming) #### 在本教程中,您將借助示例來學習 Python 中的面向對象編程(OOP)及其基本概念。 ## 面向對象編程 Python 是一種多范式編程語言。 它支持不同的編程方法。 解決編程問題的一種流行方法是創建對象。 這就是所謂的面向對象編程(OOP)。 對象具有兩個特征: * 屬性 * 行為 讓我們舉個例子: 鸚鵡可以是一個對象,因為它具有以下屬性: * 名稱,年齡,顏色作為屬性 * 唱歌,跳舞作為行為 Python 中的 OOP 概念專注于創建可重用的代碼。 此概念也稱為 DRY(不要重復自己)。 在 Python 中,OOP 的概念遵循一些基本原則: * * * ## 類 類是對象的藍圖。 我們可以將類看作是帶有標簽的鸚鵡的素描。 它包含有關名稱,顏色,大小等的所有詳細信息。基于這些描述,我們可以研究鸚鵡。 在這里,鸚鵡是一個對象。 鸚鵡類的示例可以是: ```py class Parrot: pass ``` 在這里,我們使用`class`關鍵字定義一個空類`Parrot`。 從類中,我們構造實例。 實例是從特定類創建的特定對象。 * * * ## 對象 對象(實例)是類的實例。 定義類時,僅定義對象的描述。 因此,沒有分配內存或存儲。 鸚鵡類對象的示例可以是: ```py obj = Parrot() ``` 在此,`obj`是類別`Parrot`的對象。 假設我們有鸚鵡的詳細信息。 現在,我們將展示如何構建鸚鵡的類和對象。 ### 示例 1:在 Python 中創建類和對象 ```py class Parrot: # class attribute species = "bird" # instance attribute def __init__(self, name, age): self.name = name self.age = age # instantiate the Parrot class blu = Parrot("Blu", 10) woo = Parrot("Woo", 15) # access the class attributes print("Blu is a {}".format(blu.__class__.species)) print("Woo is also a {}".format(woo.__class__.species)) # access the instance attributes print("{} is {} years old".format( blu.name, blu.age)) print("{} is {} years old".format( woo.name, woo.age)) ``` **輸出** ```py Blu is a bird Woo is also a bird Blu is 10 years old Woo is 15 years old ``` 在上面的程序中,我們創建了一個名為`Parrot`的類。 然后,我們定義屬性。 屬性是對象的特征。 這些屬性在類的`__init__`方法中定義。 創建對象后,首先運行的是初始化方法。 然后,我們創建`Parrot`類的實例。 這里,`Blu`和`woo`是我們新對象(值)的引用。 我們可以使用`__class__.species`訪問`class`屬性。 類的所有實例的類屬性都相同。 同樣,我們使用`blu.name`和`blu.age`訪問實例屬性。 但是,類的每個實例的實例屬性都不同。 要了解有關類和對象的更多信息,請轉到 [Python 類和對象](/python-programming/class) * * * ## 方法 方法是在類主體內定義的函數。 它們用于定義對象的行為。 ### 示例 2:在 Python 中創建方法 ```py class Parrot: # instance attributes def __init__(self, name, age): self.name = name self.age = age # instance method def sing(self, song): return "{} sings {}".format(self.name, song) def dance(self): return "{} is now dancing".format(self.name) # instantiate the object blu = Parrot("Blu", 10) # call our instance methods print(blu.sing("'Happy'")) print(blu.dance()) ``` **輸出**: ```py Blu sings 'Happy' Blu is now dancing ``` 在上面的程序中,我們定義了兩種方法,即`sing()`和`dance()`。 這些稱為實例方法,因為它們是在實例對象(即`blu`)上調用的。 * * * ## 繼承 繼承是一種創建新類以使用現有類的詳細信息而不進行修改的方法。 新形成的類是派生類(或子類)。 同樣,現有類是基類(或父類)。 ### 示例 3:在 Python 中使用繼承 ```py # parent class class Bird: def __init__(self): print("Bird is ready") def whoisThis(self): print("Bird") def swim(self): print("Swim faster") # child class class Penguin(Bird): def __init__(self): # call super() function super().__init__() print("Penguin is ready") def whoisThis(self): print("Penguin") def run(self): print("Run faster") peggy = Penguin() peggy.whoisThis() peggy.swim() peggy.run() ``` **輸出**: ```py Bird is ready Penguin is ready Penguin Swim faster Run faster ``` 在以上程序中,我們創建了兩個類,即`Bird`(父類)和`Penguin`(子類)。 子類繼承父類的功能。 我們可以從`swim()`方法中看到這一點。 再次,子類修改了父類的行為。 我們可以從`whoisThis()`方法中看到這一點。 此外,我們通過創建新的`run()`方法來擴展父類的功能。 另外,我們在`__init__()`方法內使用`super()`函數。 這使我們可以在子類中運行父類的`__init__()`方法。 * * * ## 封裝形式 在 Python 中使用 OOP,我們可以限制對方法和變量的訪問。 這樣可以防止數據直接修改(稱為封裝)。 在 Python 中,我們使用下劃線作為前綴來表示私有屬性,即單`_`或雙`__`。 ### 示例 4:Python 中的數據封裝 ```py class Computer: def __init__(self): self.__maxprice = 900 def sell(self): print("Selling Price: {}".format(self.__maxprice)) def setMaxPrice(self, price): self.__maxprice = price c = Computer() c.sell() # change the price c.__maxprice = 1000 c.sell() # using setter function c.setMaxPrice(1000) c.sell() ``` **輸出**: ```py Selling Price: 900 Selling Price: 900 Selling Price: 1000 ``` 在上面的程序中,我們定義了`Computer`類。 我們使用`__init__()`方法存儲`Computer`的最高售價。 我們試圖修改價格。 但是,我們無法更改它,因為 Python 將`__maxprice`視為私有屬性。 如圖所示,要更改該值,我們必須使用設置器函數,即`setMaxPrice()`,該函數將價格作為參數。 * * * ## 多態 多態是一種功能(在 OOP 中),可以將公共接口用于多種形式(數據類型)。 假設我們需要為形狀著色,有多個形狀選項(矩形,正方形,圓形)。 但是,我們可以使用相同的方法為任何形狀著色。 這個概念稱為多態。 ### 示例 5:在 Python 中使用多態 ```py class Parrot: def fly(self): print("Parrot can fly") def swim(self): print("Parrot can't swim") class Penguin: def fly(self): print("Penguin can't fly") def swim(self): print("Penguin can swim") # common interface def flying_test(bird): bird.fly() #instantiate objects blu = Parrot() peggy = Penguin() # passing the object flying_test(blu) flying_test(peggy) ``` **輸出**: ```py Parrot can fly Penguin can't fly ``` 在上面的程序中,我們定義了兩個類別`Parrot`和`Penguin`。 它們每個都有一個通用的`fly()`方法。 但是,它們的功能不同。 要使用多態,我們創建了一個通用接口,即`flying_test()`函數,該函數接受任何對象并調用該對象的`fly()`方法。 因此,當我們在`flying_test()`函數中傳遞`blu`和`peggy`對象時,它有效地運行了。 * * * ## 要記住的要點: * 面向對象的編程使程序易于理解并且高效。 * 由于該類是可共享的,因此可以重用該代碼。 * 數據通過數據抽象是安全的。 * 多態允許相同的接口用于不同的對象,因此程序員可以編寫高效的代碼。
                  <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>

                              哎呀哎呀视频在线观看