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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python 繼承與多態 > 原文: [https://thepythonguru.com/python-inheritance-and-polymorphism/](https://thepythonguru.com/python-inheritance-and-polymorphism/) * * * 于 2020 年 1 月 7 日更新 * * * 繼承允許程序員首先創建一個通用類,然后再將其擴展為更專業的類。 它還允許程序員編寫更好的代碼。 使用繼承,您可以繼承所有訪問數據字段和方法,還可以添加自己的方法和字段,因此繼承提供了一種組織代碼的方法,而不是從頭開始重寫代碼。 在面向對象的術語中,當`X`類擴展了`Y`類時,則`Y`被稱為*超類*或*基類*,而`X`被稱為*子類或派生類*。 還有一點要注意,子類只能訪問非私有的數據字段和方法,私有數據字段和方法只能在該類內部訪問。 創建子類的語法是: ```py class SubClass(SuperClass): # data fields # instance methods ``` 讓我們以一個例子來說明這一點。 ```py class Vehicle: ? ? def __init__(self, name, color): ? ? ? ? self.__name = name ? ? ?# __name is private to Vehicle class self.__color = color ? ? def getColor(self): ? ? ? ? # getColor() function is accessible to class Car ? ? ? ? return self.__color ? ? def setColor(self, color): ?# setColor is accessible outside the class ? ? ? ? self.__color = color ? ? def getName(self): # getName() is accessible outside the class ? ? ? ? return self.__name class Car(Vehicle): ? ? def __init__(self, name, color, model): # call parent constructor to set name and color ? ? ? ? super().__init__(name, color) ? ? ? ? ? self.__model = model ? ? def getDescription(self): ? ? ? ? return self.getName() + self.__model + " in " + self.getColor() + " color" # in method getDescrition we are able to call getName(), getColor() because they are # accessible to child class through inheritance c = Car("Ford Mustang", "red", "GT350") print(c.getDescription()) print(c.getName()) # car has no method getName() but it is accessible through class Vehicle ``` **預期輸出**: ```py Ford MustangGT350 in red color Ford Mustang ``` 在這里,我們創建了基類`Vehicle`及其子類`Car`。 注意,我們沒有在`Car`類中定義`getName()`,但是我們仍然可以訪問它,因為類`Car`從`Vehicle`類繼承。 在上面的代碼中,`super()`方法用于調用基類的方法。 這是`super()`的工作方式 假設您需要在子類的基類中調用名為`get_information()`的方法,則可以使用以下代碼進行調用。 ```py super().get_information() ``` 同樣,您可以使用以下代碼從子類構造器中調用基類構造器。 ```py super().__init__() ``` ## 多重繼承 * * * 與 Java 和 C# 等語言不同,python 允許多重繼承,即您可以同時從多個類繼承, ```py class Subclass(SuperClass1, SuperClass2, ...): ? ?# initializer ? # methods ``` 讓我們舉個例子: ```py class MySuperClass1(): ? ? def method_super1(self): ? ? ? ? print("method_super1 method called") class MySuperClass2(): ? ? def method_super2(self): ? ? ? ? print("method_super2 method called") class ChildClass(MySuperClass1, MySuperClass2): ? ? def child_method(self): ? ? ? ? print("child method") c = ChildClass() c.method_super1() c.method_super2() ``` **預期輸出**: ```py method_super1 method called method_super2 method called ``` 如您所見,因為`ChildClass`繼承了`MySuperClass1`,`MySuperClass2`,所以`ChildClass`的對象現在可以訪問`method_super1()`和`method_super2()`。 ## 覆蓋方法 * * * 要覆蓋基類中的方法,子類需要定義一個具有相同簽名的方法。 (即與基類中的方法相同的方法名稱和相同數量的參數)。 ```py class A(): ? ? def __init__(self): ? ? ? ? self.__x = 1 ? ? def m1(self): ? ? ? ? print("m1 from A") class B(A): ? ? def __init__(self): ? ? ? ? self.__y = 1 ? ? def m1(self): ? ? ? ? print("m1 from B") c = B() c.m1() ``` **預期輸出**: ```py m1 from B ``` 在這里,我們從基類中重寫`m1()`方法。 嘗試在`B`類中注釋`m1()`方法,現在將運行`Base`類中的`m1()`方法,即`A`類。 **預期輸出**: ```py m1 from A ``` ## `isinstance()`函數 * * * `isinstance()`函數用于確定對象是否為該類的實例。 **語法**: `isinstance(object, class_type)` ```py >>> isinstance(1, int) True >>> isinstance(1.2, int) False >>> isinstance([1,2,3,4], list) True ``` 下一章[異常處理](/python-exception-handling/)。 * * * * * *
                  <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>

                              哎呀哎呀视频在线观看