Head First Java第2章练习泳池迷宫

来源:互联网 发布:java multimap 编辑:程序博客网 时间:2024/04/30 22:56

为了输出对比更明显,稍稍更改了下:

1、最后输出e2.count=10代码:

public class EchoTest{public static void main(String[] args){Echo e1=new Echo();Echo e2=new Echo();int x=0;while(x<4){System.out.print("此时x="+x+"\t\t");e1.hello();e1.count=e1.count+1;if(x==3){e2.count=e2.count+1;}if(x>0){e2.count=e2.count+e1.count;}x=x+1;System.out.println("e1.count = "+e1.count+"\te2.count = "+e2.count);}}}class Echo{int count=0;void hello(){System.out.println("helloooooooooooo...");}}
结果:

2、最后输出e2.count=24代码:

public class EchoTest{public static void main(String[] args){Echo e1=new Echo();Echo e2=e1;int x=0;while(x<4){System.out.print("此时x="+x+"\t\t");e1.hello();e1.count=e1.count+1;if(x==3){e2.count=e2.count+1;}if(x>0){e2.count=e2.count+e1.count;}x=x+1;System.out.println("e1.count = "+e1.count+"\te2.count = "+e2.count);}}}class Echo{int count=0;void hello(){System.out.println("helloooooooooooo...");}}
结果:

3、区别:

改动了e2的声明。

1中e2的声明是Echo e2=new Echo();

2中e2的声明是Echo e2=e1;




原创粉丝点击