Java 中的單一層次繼承


單一層次繼承 - 類可從單一類繼承屬性。例如,B 類繼承 A 類。

示例

 即時演示

class Shape {
   public void display() {
      System.out.println("Inside display");
   }
}
class Rectangle extends Shape {
   public void area() {
      System.out.println("Inside area");
   }
}
public class Tester {
   public static void main(String[] arguments) {
      Rectangle rect = new Rectangle();
      rect.display();
      rect.area();
   }
}

輸出

Inside display
Inside area

此處 Rectangle 類繼承 Shape 類並且可以執行兩種方法,display() 和 area()(如所示)。

更新於: 30-Jul-2019

12K+ 次觀看

開啟你的職業生涯

完成課程以獲得認證

開始入門
廣告
© . All rights reserved.