java遺珠之隱藏和覆蓋
版權宣告:本文為博主原創文章,未經博主允許不得轉載。https://blog.csdn.net/lastsweetop/article/details/82857467
1.只有例項方法有覆蓋,類方法,類變數和例項變數都是隱藏。
2.覆蓋的意思就是會呼叫具體的子類,而隱藏就看誰去呼叫。
3.類變數可以隱藏例項變數,例項變數也可也隱藏類變數。
4.類方法和例項方法無法相互覆蓋和隱藏。
例子如下:
class Father { int a = 1; static void b() { System.out.println("father b"); } } public class Son extends Father { static int a = 2; static void b() { System.out.println("son b"); } public static void main(String[] args) { Father f = new Son(); System.out.println(f.a); f.b(); Son s=new Son(); System.out.println(s.a); s.b(); } }