Java 繼承后成員的隱藏與重寫(示例詳解)
Java 繼承后成員的隱藏與重寫
1、子類沒有定義成員
- BaseCommonStore.java
public class BaseCommonStore {
public static final String TAG = "TAG:" + BaseCommonStore.class.getSimpleName();
public static void sayHello() {
System.out.println(TAG + " sayHello");
}
public String tag = "tag:" + BaseCommonStore.class.getSimpleName();
public void sayOk() {
System.out.println(tag + " sayOk");
}
}- CommonStore.java
public class CommonStore extends BaseCommonStore {
}
- test
System.out.println(CommonStore.TAG); CommonStore.sayHello(); CommonStore commonStore = new CommonStore(); System.out.println(commonStore.tag); commonStore.sayOk();
# 輸出結(jié)果 TAG:BaseCommonStore TAG:BaseCommonStore sayHello tag:BaseCommonStore tag:BaseCommonStore sayOk
2、子類定義同名成員
- BaseCommonStore.java
public class BaseCommonStore {
public static final String TAG = "TAG:" + BaseCommonStore.class.getSimpleName();
public static void sayHello() {
System.out.println(TAG + " sayHello");
}
public String tag = "tag:" + BaseCommonStore.class.getSimpleName();
public void sayOk() {
System.out.println(tag + " sayOk");
}
}- CommonStore.java
public class CommonStore extends BaseCommonStore {
public static final String TAG = "TAG:" + CommonStore.class.getSimpleName();
public static void sayHello() {
System.out.println(TAG + " sayHello");
}
public String tag = "tag:" + CommonStore.class.getSimpleName();
public void sayOk() {
System.out.println(tag + " sayOk");
}
}- test
System.out.println(CommonStore.TAG); CommonStore.sayHello(); CommonStore commonStore = new CommonStore(); System.out.println(commonStore.tag); commonStore.sayOk();
# 輸出結(jié)果 TAG:CommonStore TAG:CommonStore sayHello tag:CommonStore tag:CommonStore sayOk
小結(jié)
| 成員 | 示例 1 | 示例 2 |
|---|---|---|
| 靜態(tài)變量 | 繼承父類 | 隱藏父類 |
| 靜態(tài)方法 | 繼承父類 | 隱藏父類 |
| 實(shí)例變量 | 繼承父類 | 隱藏父類 |
| 實(shí)例方法 | 繼承父類 | 重寫父類 |
到此這篇關(guān)于Java 繼承后成員的隱藏與重寫(示例詳解)的文章就介紹到這了,更多相關(guān)java隱藏與重寫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis中resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置方式
這篇文章主要介紹了Mybatis中resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot配置Druid數(shù)據(jù)監(jiān)控代碼實(shí)例
這篇文章主要介紹了SpringBoot配置Druid數(shù)據(jù)監(jiān)控代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Selenium+Tesseract-OCR智能識(shí)別驗(yàn)證碼爬取網(wǎng)頁數(shù)據(jù)的實(shí)例
本文主要介紹了Selenium+Tesseract-OCR智能識(shí)別驗(yàn)證碼爬取網(wǎng)頁數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
關(guān)于Mybatis插入對(duì)象時(shí)空值的處理
這篇文章主要介紹了關(guān)于Mybatis插入對(duì)象時(shí)空值的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
SpringBoot操作Mongodb的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot操作Mongodb的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06

