java扫盲--java继承关系

抽出时间再过一遍java基础,记录自己之前没有学会或没有注意到的知识点

 

java继承关系相关

先说结论

1:父类声明的变量和子类声明的变量同时存在,不会直接覆盖

2:父类被重写的方法,在子类中无法访问,但变量可以

3:静态方法不具备多态性

 

测试代码如下:

        @Test
	public void testParent(){
		Child child=new Child();
		System.out.println("----showMe--begin---");
		child.showMe();
		System.out.println("----showMe--end---");
		System.out.println("----child.var---");
		System.out.println(child.var);
		System.out.println("----child.doSome()---");
		child.doSome();
		System.out.println("----((Parent)child).var---");
		System.out.println(((Parent)child).var);
		System.out.println("----((Parent)child).doSome()---");
		((Parent)child).doSome();
		System.out.println("----((GrandParent)child).var---");
		System.out.println(((GrandParent)child).var);
		System.out.println("----((GrandParent)child).doSome()---");
		((GrandParent)child).doSome();
		System.out.println("----静态方法不具备多态性---");
		Parent p=new Child();
		p.staticDoSome();
		((GrandParent)p).staticDoSome();
		p.doSome();
		((GrandParent)p).doSome();
	}
public class Child extends Parent {
    protected String var="-Child";

    protected final String varFinal="-ChildParentFinal";

    public static void  staticDoSome(){
        System.out.println("Child staticDoSome");
    }
    @Override
    protected void doSome(){
        System.out.println("Child,doSome"+var);
    }
    public void showMe(){
        this.doSome();
        super.doSome();
        ((Parent)this).doSome();
        ((GrandParent)this).doSome();
        System.out.println("Child this.var--"+this.var);
        System.out.println("Child ((Parent)this).var--"+((Parent)this).var);
        System.out.println("Child ((GrandParent)this).var--"+((GrandParent)this).var);
        System.out.println("Child this.staticDoSome()--");
        this.staticDoSome();
        System.out.println("Child super.staticDoSome()--");
        super.staticDoSome();
    }
}

 

public class Parent extends GrandParent {
    protected String var="-Parent";

    protected final String varFinal="-ParentFinal";

    public static void  staticDoSome(){
        System.out.println("Parent staticDoSome");
    }
    @Override
    protected void doSome(){
        System.out.println("Parent,doSome"+var);
    }
}
public class GrandParent {
    protected String var="-GrandParent";

    protected final String varFinal="-GrandParentFinal";

    public static void  staticDoSome(){
        System.out.println("GrandParent staticDoSome");
    }

    protected void doSome(){
        System.out.println("GrandParent,doSome"+var);
    }
}

最后测试结果如下:

----showMe--begin---
Child,doSome-Child
Parent,doSome-Parent
Child,doSome-Child
Child,doSome-Child
Child this.var---Child
Child ((Parent)this).var---Parent
Child ((GrandParent)this).var---GrandParent
Child this.staticDoSome()--
Child staticDoSome
Child super.staticDoSome()--
Parent staticDoSome
----showMe--end---
----child.var---
-Child
----child.doSome()---
Child,doSome-Child
----((Parent)child).var---
-Parent
----((Parent)child).doSome()---
Child,doSome-Child
----((GrandParent)child).var---
-GrandParent
----((GrandParent)child).doSome()---
Child,doSome-Child
----静态方法不具备多态性---
Parent staticDoSome
GrandParent staticDoSome
Child,doSome-Child
Child,doSome-Child


回头,来看看原因

首先对于第一点,debug一下

此时,child对象的内容如下

它的变量 var的值同时存在三个,分别是自己的var以及从父类继承下来的var,

如果想要访问父类的变量(重名,子类中有同名变量)该怎么办?

这里用到强转,比如:((Parent)child).var,结果在后台打印可以看到.

 

再看第二点:父类被重写的方法,在子类中无法访问

代码运行到这里时

后台打印出来的结果是 Child,doSome-Child

而到这里时

本以为会打印出 Parent,doSome-Parent,而实际上却是 Child,doSome-Child

运行到这时

没错,依旧是Child,doSome-Child

由此看出new Child 对象无法访问它的父类方法doSome(),因为被重写了

再看第三点 静态方法不具备多态性

这段代码的运行结果是

Parent staticDoSome
GrandParent staticDoSome
Child,doSome-Child
Child,doSome-Child

这里可以看出,非静态方法依旧是被重写后的输出结果,但静态方法却是父类的输出结果,因为静态方法是和类相关联的,它在被创建的时候就已经被决定是属于谁的.final关键词其实也是同理.

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值