在 Java 中,如何根据方法的情况返回字符串或双精度值?是否可以?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3947697/
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
In Java, How do I return a String or a double depending on the circumstance of the method? Is it possible?
提问by trusktr
For example, I have a method that looks through a string for data separated by a specified deliminator, but some items might be a names, and other items might be numbers.
例如,我有一个方法可以通过字符串查找由指定分隔符分隔的数据,但有些项目可能是名称,而其他项目可能是数字。
If a user calls my method to return item number X from the deliminated list, i want it to return a string if item X is a name, or a double if item X is a number.
如果用户调用我的方法从分隔列表中返回项目编号 X,如果项目 X 是名称,我希望它返回一个字符串,如果项目 X 是一个数字,我希望它返回一个字符串。
For example, objectName.get(5);
would get the 5th item in the deliminated list.
例如,objectName.get(5);
将获得分隔列表中的第 5 个项目。
Would I have to use some type of overloading for this?
我是否必须为此使用某种类型的重载?
Or would I have to instead do something like objectName.getDouble(5);
and objectName.getString(5);
based on the fact that the user knows what item 5 is?
或者,我会代替做这样的事情objectName.getDouble(5);
,并objectName.getString(5);
基于用户知道什么项目5是事实?
But what if the user doesn't know what item 5 is? He just needs a String or a Double depending on what it happens to be.
但是如果用户不知道第 5 项是什么怎么办?他只需要一个 String 或一个 Double ,这取决于它碰巧是什么。
回答by Stephen C
Here's one way to do this:
这是执行此操作的一种方法:
public Object get() {
if (blueMoon) {
return new Double(42.0);
} else {
return "fred";
}
}
Note that this will return a Double
wrapper rather than a double
.
请注意,这将返回一个Double
包装器而不是double
.
I don't think this is a good idea though, since the caller now has to test the type of the returned value and do a typecast to do something with it.
不过,我认为这不是一个好主意,因为调用者现在必须测试返回值的类型并进行类型转换以对其进行处理。
For the record, Java does not allow a method to return a String
or double
because these types do not have a common supertype in the Java type system.
作为记录,Java 不允许方法返回 aString
或者double
因为这些类型在 Java 类型系统中没有通用的超类型。
回答by Stephen C
The Java language does not expose an overload on the return type of a method.(As Thilo pointed out, this is a restriction of the Java language and not the JVM/bytecode.)
Java 语言不公开方法返回类型的重载。(正如 Thilo 指出的,这是 Java 语言的限制,而不是 JVM/字节码。)
Generally this type of thing does not fit well into the Java type system. One could imagine returning an Either<String,Double>
type (a more restricted return type than Object
as suggested by Stephen C and a more general type than DoubleOrString as pointed out by B. Bear), but the general effort required to use such a construct in Java generally results in simply having multiple methods, e.g. getString(...)
and getDouble(...)
.
通常这种类型的东西不太适合 Java 类型系统。可以想象返回一个Either<String,Double>
类型(比Object
Stephen C 建议的更受限制的返回类型和比 B. Bear 指出的 DoubleOrString 更通用的类型),但是在 Java 中使用这种构造所需的一般努力通常会导致简单的结果有多种方法,例如getString(...)
和getDouble(...)
。
回答by Burleigh Bear
For this sort of thing, I prefer to use something akin to the Maybe/Option pattern from the functional programming camp. You end up with an interface like:
对于这类事情,我更喜欢使用类似于函数式编程阵营中的 Maybe/Option 模式的东西。你最终会得到一个界面,如:
public abstract class DoubleOrString
{
// Constraint isDouble() xor isString()
public boolean isDouble();
public boolean isString();
//Must throw iff !isString()
public String getString();
//Must throw iff !ifDouble()
public Double getDouble();
public static DoubleOrString wrap(final double wrapMe)
{
return new DoubleOrString()
{
public boolean isDouble() {return true;}
public boolean isString() {return false;}
public Double getDouble() {return wrapMe;}
public String getString() {throw new RuntimeException();}
};
}
//same for wrap(String)
}
This forces the issue for clients, in that there is always a sanity check that there was indeed a double or String at the appropriate time. In your case, I'd make just one get() method, so when the client (thinks they) knows what the type is, the call is
这迫使客户端出现问题,因为总是有一个健全的检查,在适当的时间确实有一个 double 或 String。在你的情况下,我只做一个 get() 方法,所以当客户端(认为他们)知道类型是什么时,调用是
objectName.get(5).getString();
and in your get(int) method, rather than returning a String or a double, the return statement looks like
在你的 get(int) 方法中,return 语句看起来像
DoubleOrString.wrap(theThingToReturn)
It's a little extra work up front, but it has paid of for me several times in the past.
这是一些额外的前期工作,但过去它已经为我付出了好几次。
Here's how you'd use it to build one (warning - this hasn't been near a compiler)
这是你如何使用它来构建一个(警告 - 这还没有接近编译器)
public static DoubleOrString parseADoubleOrString(String input) {
try {
return DoubleOrString.wrap(Integer.parseInt(input))
} catch (NumberFormatException nfe) {
return DoubleOrString.wrap(input);
}
}
and here's what the client looks like
这是客户端的样子
String input = //get the input from the user somehow
DoubleOrString parsed = parseADoubleOrString(input);
if (parsed.isDouble())
aFunctionThatTakesADouble(parsed.getDouble());
else
aFunctionThatTakesAString(parsed.getString());
回答by cherouvim
If you need to do this then there is problem with your design. Since the original datasource is String you have to accept that all returned values will be string and leave it to the client to check whether the result can be converted to a number.
如果您需要这样做,那么您的设计就有问题。由于原始数据源是字符串,因此您必须接受所有返回的值都是字符串并将其留给客户端检查结果是否可以转换为数字。
If you want to save the client from doing the check, you can provide him with a minimal API which may look something like:
如果您想避免客户端进行检查,您可以为他提供一个最小的 API,它可能如下所示:
public class ValueExtractor {
public ValueExtractor(String delimitedText) {
// ...
}
/**
* Determines whether there is a next element
* to be returned
*/
public boolean next() {
// ...
}
public String get() {
// ...
}
/**
* Returns the value as a Double if possible
* null otherwise.
*/
public Double getPossibleDouble() {
// ...
}
}