Re: [问题] 取得基本型别的reference

楼主: adrianshum (Alien)   2014-07-19 11:55:02
※ 引述《lordfish62 (阿瑜)》之铭言:
: 版上的各位好,
: 想请教各位如何在java中取得基本型别如:int, char, ...
: 的reference呢?
: 写个简单的java code如下:
: public class Test
: {
: public void foo(int m)
: {
: m += 1;
: System.out.println(m);
: }
: public static void main(String[] args)
: {
: int temp = 0;
: System.out.println(temp);
: new Test().foo(temp);
: System.out.println(temp);
: }
: }
: 输出的结果是:
: 0
: 1
: 0
: 因为在java中基本型别的变量名称并不是reference
: 我想问的是如何可以取得temp的reference传进函式里
: 使得输出变成:
: 0
: 1
: 1
: 解释的有点乱,不知道我的表达是否清楚==
没有直接的方法,唯一可以做的 workaround 只有类似这种:
class IntHolder {
int i = 0;
public IntHolder(int i) {
this.value = i;
}
// getter, setter
}
class Test {
public void foo(IntHolder intHolder) {
intHolder.setValue(intHolder.getValue() + 1);
}
public static void main(String[] args)
{
IntHolder temp = new IntHolder(0);
System.out.println(temp.getValue());
new Test().foo(temp);
System.out.println(temp.getValue());
}
}
作者: Killercat (杀人猫™)   2014-07-19 23:23:00
直接用这个吧http://tinyurl.com/nl52qxt不过这东西本来用途不是这个就是 反正都写好了 XD这本来是为了解决final native_type的一些问题做的
作者: NullLife (废材大叔有点累)   2014-07-20 01:06:00
推文的类别颇酷,推一个~

Links booklink

Contact Us: admin [ a t ] ucptt.com