| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5713 人关注过本帖
标题:考大家一道题(把我迷糊了) 答对"有奖"!
只看楼主 加入收藏
maoxiaolu
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2006-6-15
收藏
得分:0 

有道理,这题出的好。


代码太难只能硬看- -!
2006-07-07 11:22
wxhwjsw
Rank: 1
等 级:新手上路
帖 子:274
专家分:0
注 册:2006-6-15
收藏
得分:0 

看了半天还是没明白,  我想不通的是他怎么会三个输出的???


一个人的快乐,不是因为他拥有的多,而是因为他计较的少.
2006-07-09 10:15
Esavi
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-7-5
收藏
得分:0 
以下是引用★王者至尊★在2006-5-6 12:51:00的发言:
选择下面这段代码的输出结果:
-----------------------------------------------------------------
class Test{
public int i=10; //instance variable,for every object of type Test there's a
//copy of i

}
public class ObParm{
public static void main(String argv[]){
ObParm o=new ObParm(); //invoke the constructor of ObParm
o.amethod(); //invoke the instance method with o
}
public void amethod(){
int i=99; //local variable,which will die once the method is
//finished
Test v=new Test(); //invoke the default constructor of Test
v.i=30; // instance variable v.i set to 30
another(v,i); //invoke another(Test v,int i)
System.out.println(v.i); //output
}
public void another(Test v, int i){
i=0; //another local variable with the same name with the local
//variable in amethod() and
//the instance variable in Test
v.i=20; //set v.i to 20.This value will last even when the method is finished
//because when we pass an object argument to a
//method the reference to the object is
//copied to the parameter,which will of course refer to the same object.
Test vh=new Test(); //invoke the default constructor of Test,which will automatically set i to 10
v=vh; //now v.i is 10 instead of 20,but we didn't not change the
//i in the original object
//Remember,it is the reference to the object that is copied to the method.
//So,you are just changing a copy of the reference to
//the original object here
//which will not change the original object at all!
System.out.println(v.i+" "+i); //output
}
}

----------------------------------------------------------
A 10,0,30
B 20,0,30
C 20,99,30
D10,0,20 is the answer
(不许运行哦!)

至于为什么会输出三个数字,请注意System.out.println(v.i+" "+i);是输出两个数字的

[此贴子已经被作者于2006-7-9 11:20:34编辑过]


向雨之傷
2006-07-09 11:18
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
呵呵,有意思,差点就选择错了,关键是题目的代码编排容易误导人呵呵

淘宝杜琨
2006-07-09 14:48
☆王者至尊☆
Rank: 1
等 级:新手上路
威 望:1
帖 子:43
专家分:0
注 册:2006-6-21
收藏
得分:0 
呵呵 这贴又被大家顶上来了 真是不容易啊

2006-07-10 23:10
panda_cn
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-7-10
收藏
得分:0 
以下是引用Esavi在2006-7-9 11:18:17的发言:

至于为什么会输出三个数字,请注意System.out.println(v.i+" "+i);是输出两个数字的



上面应该还有一句 System.out.println(v.i); 吧
在 amethod() 里面

2006-07-11 09:16
aisingioro
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-4-21
收藏
得分:0 
我dry run的结果是10,0,10,可实际运行的结果是D。搞不明白。

class Test{
public int i=10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o=new ObParm();
o.amethod();
}
public void amethod(){
int i=99;
Test v=new Test();
v.i=30;
another(v,i);
System.out.println(v.i);
}
public void another(Test v, int i){
i=0;
v.i=20;
Test vh=new Test();
v=vh;//在此处v指向vh,那v.i就应该是vh.i,以前v指向的空间已经没有引用了,可以被垃圾回收了。那么应该是10啊
System.out.println(v.i+" "+i);
}
}
2006-07-20 16:59
yjl99
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-8-28
收藏
得分:0 

好贴

2006-08-28 11:05
wxhwjsw
Rank: 1
等 级:新手上路
帖 子:274
专家分:0
注 册:2006-6-15
收藏
得分:0 
又被谁找着啦.......

一个人的快乐,不是因为他拥有的多,而是因为他计较的少.
2006-08-28 12:11
lt2046
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2006-7-21
收藏
得分:0 
class Test{
public int i=10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o=new ObParm();
o.amethod();
}
public void amethod(){
int i=99;
Test v=new Test();
v.i=30;
another(v,i);
System.out.println(v.i);
}
public void another(Test v, int i){ //但这明明是一个无返回值的方法,为什么会影响到上面的输出
i=0;
v.i=20; //为新传进来的对象属性赋值
Test vh=new Test(); //新对象i已有值10
v=vh; //新的引用
System.out.println(v.i+" "+i);
}
}

2006-08-29 22:22
快速回复:考大家一道题(把我迷糊了) 答对"有奖"!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016983 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved