黑马程序员技术交流社区
标题:
关于private方法override时内存模型问题
[打印本页]
作者:
ZYZQ
时间:
2015-5-24 03:32
标题:
关于private方法override时内存模型问题
本帖最后由 ZYZQ 于 2015-5-24 03:34 编辑
class A{
public void fun1(){
System.out.println("A-->public void fun1()");
}
public void fun2(){
fun1();
System.out.println("this is "+this);
}
}
class B extends A{
public void fun1(){
System.out.println("B-->public void fun1()");
}
public void fun3(){
System.out.println("B-->public void fun3()");
}
public void fun4(){
this.fun1();
System.out.println("this is "+this);
}
}
public class Demo{
public static void main(String[] args){
A a = new B();//多态
B b = (B)a;//强转
A aa = new A();
B bb = new B();
System.out.println("a is "+a);
System.out.println("b is "+b);
System.out.println("aa is "+aa);
b.fun1();
b.fun2();
b.fun3();
b.fun4();
a.fun2();
aa.fun2();
bb.fun2();
bb.fun1();
bb.fun4();
}
}
/**
1.当A类中fun1函数权限为private(覆写失败)时,输出结果为:
----------------------------------------------------
a is B@15db9742
b is B@15db9742
aa is A@6d06d69c
B-->public void fun1()
A-->public void fun1()
this is B@15db9742
B-->public void fun3()
B-->public void fun1()
this is B@15db9742
A-->public void fun1()
this is B@15db9742
A-->public void fun1()
this is A@6d06d69c
A-->public void fun1()
this is B@7852e922
B-->public void fun1()
B-->public void fun1()
this is B@7852e922
----------------------------------------------------
2.当A类中fun1函数权限为public(覆写成功)时,输出结果为:
a is B@15db9742
b is B@15db9742
aa is A@6d06d69c
B-->public void fun1()
B-->public void fun1()
this is B@15db9742
B-->public void fun3()
B-->public void fun1()
this is B@15db9742
B-->public void fun1()
this is B@15db9742
A-->public void fun1()
this is A@6d06d69c
B-->public void fun1()
this is B@7852e922
B-->public void fun1()
B-->public void fun1()
this is B@7852e922
*/
复制代码
当A类中fun1()权限为private或public时,b.fun2()的输出结果是不同的,求教内存中是如何变化带来的这种差异?
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2