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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 繼承示例 > 原文: [https://javatutorial.net/java-inheritance-example](https://javatutorial.net/java-inheritance-example) 此示例演示了 Java 編程語言中繼承的用法 ## 什么是繼承 繼承是一種 OOP 功能,它允許 Java 類從其他類派生。 父類稱為超類,而派生類稱為子類。 子類從其超類繼承字段和方法。 繼承是[面向對象編程(OOP)](https://javatutorial.net/java-oop)背后的四個主要概念之一。 OOP 問題在工作面試中很常見,因此您可能會在下一次 Java 工作面試中遇到有關繼承的問題。 Java 中的“所有類的母親”是`Object`類。 Java 中的每個類都繼承自`Object`。 在層次結構的頂部,`Object`是所有類中最通用的。 層次結構底部附近的類提供了更特殊的行為。 ![All Classes in the Java Platform are Descendants of Object](https://img.kancloud.cn/07/79/07799e9b107424cdf1abc3c661435efd_553x287.jpg) Java 平臺中的所有類都是對象的后代(圖像來自 [Oracle](https://oracle.com) ) Java 具有單個繼承模型,這意味著每個類都只有一個并且只有一個直接超類。 子類繼承其父級的所有公共和受保護的成員,無論該子類位于哪個包中。如果該子類與其父級位于同一包中,則它也繼承父成員的私有成員。 您可以按原樣使用繼承的成員,替換它們,隱藏它們,或用新成員補充它們: * 繼承的字段可以像其他任何字段一樣直接使用。 * 您可以在子類中聲明一個與超類中的名字相同的字段,因此隱藏了(不推薦)。 * 您可以在子類中聲明不在超類中的新字段。 * 繼承的方法可以直接使用。 * 您可以在子類中編寫一個新的實例方法,該方法具有與超類中的簽名相同的簽名,因此將覆蓋。 * 您可以在子類中編寫一種新的靜態方法,該方法具有與超類中的簽名相同的簽名,因此隱藏了。 * 您可以在子類中聲明不在超類中的新方法。 * 您可以編寫一個隱式或使用關鍵字`super`來調用超類的構造函數的子類構造函數。 繼承是一種強大的技術,可讓您編寫干凈且可維護的代碼。 例如,假設您有一個具有多個后繼類的超類。 更改超類中的幾行代碼,并更改每個繼承者的功能,而不是在每個子類的每一端這樣做,要容易得多。 ## Java 繼承示例 在下面的示例中,我們創建 3 個類。 超類`Point`表示二維空間中具有`x`和`y`坐標的點。 ```java package net.javatutorial; public class Point { // fields marking X and Y position of the point public int x; public int y; // one constructor public Point(int x, int y) { super(); this.x = x; this.y = y; } // getter and setter methods public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } } ``` `ColoredPoint`是一個子類,它擴展了`Point`的所有屬性和方法,并添加了一個附加字段 – `colorName`。 注意這是如何完成的–我們使用關鍵字`extends`來告訴我們要從哪個類派生 ```java package net.javatutorial; public class ColoredPoint extends Point { // new field added to store the color name public String colorName; public ColoredPoint(int x, int y, String colorName) { super(x, y); this.colorName = colorName; } public String getColorName() { return colorName; } public void setColorName(String colorName) { this.colorName = colorName; } } ``` 最后是一個測試繼承的程序。 首先,我們創建一個類型為`ColoredPoint`的新`Point`。 請注意關鍵字`instanceof`的用法。 這樣,我們可以檢查對象是否為某種類型。 一旦確定點的類型為`ColoredPoint`,就可以使用以下方法顯式地進行類型轉換: ```java ColoredPoint coloredPoint = (ColoredPoint)point; ``` 現在我們可以訪問新屬性`colorName` ```java package net.javatutorial; public class InheritanceExample { public static void main(String[] args) { Point point = new ColoredPoint(2, 4, "red"); if (point instanceof ColoredPoint) { ColoredPoint coloredPoint = (ColoredPoint)point; System.out.println("the color of the point is: " + coloredPoint.getColorName()); System.out.println("with coordinates x=" + coloredPoint.getX() + " y=" + coloredPoint.getY()); } } } ``` 運行上面的示例將產生以下輸出 ```java the color of the point is: red with coordinates x=2 y=4 ``` ### 參考文獻 官方 Oracle [繼承教程](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)
                  <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>

                              哎呀哎呀视频在线观看