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();
# 输出结果 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();
# 输出结果 tag:commonstore tag:commonstore sayhello tag:commonstore tag:commonstore sayok
小结
| 成员 | 示例 1 | 示例 2 | 
|---|---|---|
| 静态变量 | 继承父类 | 隐藏父类 | 
| 静态方法 | 继承父类 | 隐藏父类 | 
| 实例变量 | 继承父类 | 隐藏父类 | 
| 实例方法 | 继承父类 | 重写父类 | 
到此这篇关于java 继承后成员的隐藏与重写(示例详解)的文章就介绍到这了,更多相关java隐藏与重写内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
 
             我要评论
我要评论 
                                             
                                             
                                             
                                             
                                            
发表评论