面试对象你真的明白吗?

public class Test {
	public static void main(String[] args) {

		
		A a1 = new A();  
        A a2 = new B();  
        B b = new B();  
        C c = new C();   
        D d = new D();   
        
         //调用的顺序是
         //this.show(O)、super.show(O)、this.show((super)O)、super.show((super)O)
        
        //输出:A and A
        //先找A中的show没有show(b)的方法,再找super.show(b),A没有父类,
        //再继续找this.show((super)b),由于b继承了A,这里就找到了所以这里调用了A的show(A)方法
        System.out.println(a1.show(b));  
        
        //输出:A and A
        //同样,先找A中的show没有show(c)的方法,再找super.show(c),A没有父类,
        //再继续找this.show((super)c),由于c继承了B,而B继承了A,所以这里还是调用了a的show(a)
        System.out.println(a1.show(c));   
        
        //输出:A and D
        //同样,先找A中的show没有show(d)的方法,再找super.show(d)这里就已经找到了
        System.out.println(a1.show(d));   
        
        //输出:B and A
        //先找A中的show没有show(b)的方法,再找super.show(b),A没有父类,
        //再继续找this(a2).show((super)b),由于b继承了A,这里就找到了所以这里调用了A的show(A)方法
        //a2.show(b),a2是一个引用变量,类型为A,则this为a2,b是B的一个实例,于是它到类A里面找show(B obj)方法,
        //没有找到,于是到A的super(超类)找,而A没有超类,因此转到第三优先级this.show((super)O),
        //this仍然是a2,这里O为B,(super)O即(super)B即A,因此它到类A里面找show(A obj)的方法,
        //类A有这个方法,但是由于a2引用的是类B的一个对象,B覆盖了A的show(A obj)方法,因此最终锁定到类B的show(A obj),输出为"B and A”。
        System.out.println(a2.show(b));  
        
        //输出:B and A
        System.out.println(a2.show(c));   
        
        //输出:A and D
        System.out.println(a2.show(d));   
        
        //输出:B and B
        System.out.println(b.show(b));    
        //输出:B and B
        //b.show(c),b是一个引用变量,类型为B,则this为b,c是C的一个实例,
        //于是它到类B找show(C obj)方法,没有找到,转而到B的超类A里面找,A里面也没有,
        //因此也转到第三优先级this.show((super)O),this为b,O为C,(super)O即(super)C即B,
        //因此它到B里面找show(B obj)方法,找到了,由于b引用的是类B的一个对象,因此直接锁定到类B的show(B obj),输出为"B and B”。
        System.out.println(b.show(c));    
        
        //输出:A and D
        System.out.println(b.show(d));
	}
}

class A {
	public A(){
		System.out.println("new A");
	}
	public String show(D obj) {
		//System.out.println("A:"+this.getClass().getName());
		return ("A and D");
	}

	public String show(A obj) {
		//System.out.println("A1:"+this.getClass().getName());
		return ("A and A");
	}
}

class B extends A {
	public B(){
		System.out.println("new B");
	}
	public String show(B obj) {
		//System.out.println("B:"+this.getClass().getName());
		return ("B and B");
	}

	public String show(A obj) {
		//System.out.println("B1:"+this.getClass().getName());
		return ("B and A");
	}
}

class C extends B {
	public C(){
		super();
		//System.out.println("C:"+super.getClass().getName());
	}
}

class D extends B {
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫大叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值