Java 中的方法隱藏是什麼?如何使用?


當超類和子類包含相同的方法,包括引數,並且它們都是靜態的。

超類中的方法將被子類中的方法隱藏。這種機制被稱為方法隱藏。

示例

即時演示

class Demo{
   public static void demoMethod() {
      System.out.println("method of super class");
   }
}
public class Sample extends Demo {
   public static void demoMethod() {
      System.out.println("method of sub class");
   }
   public static void main(String args[] ) {
      Sample.demoMethod();
   }
}

輸出

method of sub class

更新日期: 2019 年 7 月 30 日

2K+ 次瀏覽

開啟您的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.