Java:修改作为参考传递的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37505473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Modifying parameter passed as a Reference
提问by Shashi
I have to implement an interface method that has the signature like:
我必须实现一个具有如下签名的接口方法:
public int runMethod(final int key, Reference <String> result);
I have to update the value of result
parameter before method returns. For example if the value of result
was ABC
when the method was invoked, I need to modify it as DEF
and return to caller. Can somebody suggest how do I achieve it?
我必须result
在方法返回之前更新参数的值。例如,如果调用方法时的值为result
was ABC
,则需要将其修改为 asDEF
并返回给调用者。有人可以建议我如何实现它吗?
回答by SamTebbs33
You can't modify the variablethat you passed to the method. For example:
您无法修改传递给该方法的变量。例如:
public int runMethod(final int key, Reference <String> result) {
result = null; // Only changed the method's version of the variable, and not the variable that was passed to the method
}
...
Reference<String> ref = ...
runMethod(0, ref);
// ref is still what you originally assigned it to
However, you can modify the fields and call the methods of the objectyou pass.
但是,您可以修改字段并调用您传递的对象的方法。
public int runMethod(final int key, Reference <String> result) {
result.someField = ...; // Here we are changing the object, which is the same object as what was passed to the method.
}
...
Reference<String> ref = ...
runMethod(0, ref);
// ref.someField has now been changed
An alternative would be to change the method's return type to Reference<String>
and return the updated value.
另一种方法是将方法的返回类型更改为Reference<String>
并返回更新后的值。
public Reference<String> runMethod(final int key, Reference <String> result) {
return ...;
}
...
Reference<String> ref = ...
ref = runMethod(0, ref);
// ref is now whatever you returned from the method
回答by richardqiao
If you really want to pass as reference, here is a tricky way by wrapping it in an array:
如果你真的想作为参考传递,这里有一个棘手的方法是将它包装在一个数组中:
class TestRef{
static void func(int[] arr){arr[0] = -arr[0];}
public static void main(String[] args){
int[] arrI = new int[1];
arrI[0] = 250;
System.out.println(arrI[0]); // 250
func(arrI);
System.out.println(arrI[0]); // -250
}
}
回答by Richard Guy
In java objects are in effect passed by reference. That is, parameter 'result' within runMethod appears to be exactly the same object as in the caller, not a copy. So, by updating the contents of Reference parameter of runMethod you will be achieving the goal you have described.
在 java 中,对象实际上是通过引用传递的。也就是说,runMethod 中的参数“result”似乎与调用者中的对象完全相同,而不是副本。因此,通过更新 runMethod 的 Reference 参数的内容,您将实现您所描述的目标。
However, 'result' itself is not actually a pointer to the caller's passed variable so it is not possible to overwrite the object itself, only update it's contents.
但是,'result' 本身实际上并不是指向调用者传递的变量的指针,因此不可能覆盖对象本身,只能更新它的内容。