如何在 Java 中使用输出参数? [英] How to use an output parameter in Java?
问题描述
有人能给我一些在函数中使用输出参数的示例代码吗?我试过谷歌它,但只是在函数中发现它.我想在另一个函数中使用这个输出值.
Could someone please give me some sample code that uses an output parameter in function? I've tried to Google it but just found it just in functions. I'd like to use this output value in another function.
我正在开发的代码旨在在 Android 中运行.
The code I am developing intended to be run in Android.
推荐答案
Java 按值传递;没有像 C# 中的 out
参数.
Java passes by value; there's no out
parameter like in C#.
您可以使用return
,也可以改变以作为引用(按值)传递的对象.
You can either use return
, or mutate an object passed as a reference (by value).
- Does Java have something like C#'s ref and out keywords? ? (NO!)
- Is Java pass by reference? (NO!)
public class FunctionSample {
static String fReturn() {
return "Hello!";
}
static void fArgNoWorkie(String s) {
s = "What am I doing???"; // Doesn't "work"! Java passes by value!
}
static void fMutate(StringBuilder sb) {
sb.append("Here you go!");
}
public static void main(String[] args) {
String s = null;
s = fReturn();
System.out.println(s); // prints "Hello!"
fArgNoWorkie(s);
System.out.println(s); // prints "Hello!"
StringBuilder sb = new StringBuilder();
fMutate(sb);
s = sb.toString();
System.out.println(s); // prints "Here you go!"
}
}
另见
- 不可变是什么意思?
- Java 中的 StringBuilder 和 StringBuffer
至于 OP 需要帮助的代码,这是一个典型的使用特殊值(对于引用类型通常为 null
)来指示成功/失败的解决方案:
As for the code that OP needs help with, here's a typical solution of using a special value (usually null
for reference types) to indicate success/failure:
代替:
String oPerson= null;
if (CheckAddress("5556", oPerson)) {
print(oPerson); // DOESN'T "WORK"! Java passes by value; String is immutable!
}
private boolean CheckAddress(String iAddress, String oPerson) {
// on search succeeded:
oPerson = something; // DOESN'T "WORK"!
return true;
:
// on search failed:
return false;
}
改用 String
返回类型,用 null
表示失败.
Use a String
return type instead, with null
to indicate failure.
String person = checkAddress("5556");
if (person != null) {
print(person);
}
private String checkAddress(String address) {
// on search succeeded:
return something;
:
// on search failed:
return null;
}
java.io.BufferedReader.readLine()
有效,例如:它返回instanceof String
(可能是一个空字符串!),直到它返回null
表示搜索"结束.
This is how java.io.BufferedReader.readLine()
works, for example: it returns instanceof String
(perhaps an empty string!), until it returns null
to indicate end of "search".
这当然不限于引用类型的返回值.关键是必须有一些特殊的值永远不是有效的值,并且您将这些值用于特殊目的.
This is not limited to a reference type return value, of course. The key is that there has to be some special value(s) that is never a valid value, and you use that value for special purposes.
另一个经典例子是String.indexOf
:返回-1
表示搜索失败.
Another classic example is String.indexOf
: it returns -1
to indicate search failure.
注意:因为Java没有输入"和输出"参数的概念,使用i-
和o-
前缀(例如 iAddress
, oPerson
)是不必要的,而且是单调的.
Note: because Java doesn't have a concept of "input" and "output" parameters, using the
i-
ando-
prefix (e.g.iAddress
,oPerson
) is unnecessary and unidiomatic.
<小时>
更通用的解决方案
如果您需要返回多个值,通常它们以某种方式相关(例如x
和y
单个Point
的坐标).最好的解决方案是将这些值封装在一起.人们使用过 Object[]
或 List
,或通用的 Pair
,但实际上,您自己的类型最好.
A more general solution
If you need to return several values, usually they're related in some way (e.g. x
and y
coordinates of a single Point
). The best solution would be to encapsulate these values together. People have used an Object[]
or a List<Object>
, or a generic Pair<T1,T2>
, but really, your own type would be best.
对于这个问题,我推荐一个像这样的不可变的SearchResult
类型来封装boolean
和String
的搜索结果:
For this problem, I recommend an immutable SearchResult
type like this to encapsulate the boolean
and String
search results:
public class SearchResult {
public final String name;
public final boolean isFound;
public SearchResult(String name, boolean isFound) {
this.name = name;
this.isFound = isFound;
}
}
然后在您的搜索功能中,您执行以下操作:
Then in your search function, you do the following:
private SearchResult checkAddress(String address) {
// on address search succeed
return new SearchResult(foundName, true);
:
// on address search failed
return new SearchResult(null, false);
}
然后你像这样使用它:
SearchResult sr = checkAddress("5556");
if (sr.isFound) {
String name = sr.name;
//...
}
如果您愿意,您可以(并且可能应该)将 final
不可变字段设为非 public
,并使用 public
getter 代替.
If you want, you can (and probably should) make the final
immutable fields non-public
, and use public
getters instead.
这篇关于如何在 Java 中使用输出参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!