Java泛型返回类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2669326/
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 generic return type
提问by Colby77
I'd like to write a method that can accept a type param (or whatever the method can figure out the type from) and return a value of this type so I don't have to cast the return type.
我想编写一个可以接受类型参数(或任何方法可以从中找出类型的方法)并返回此类型值的方法,因此我不必强制转换返回类型。
Here is a method:
这是一个方法:
public Object doIt(Object param){
if(param instanceof String){
return "string";
}else if(param instanceof Integer){
return 1;
}else{
return null;
}
}
When I call this method, and pass in it a String, even if I know the return type will be a String I have to cast the return Object. This is similar to the int param.
当我调用此方法并传入一个字符串时,即使我知道返回类型将是一个字符串,我也必须强制转换返回对象。这类似于 int 参数。
How shall I write this method to accept a type param, and return this type?
我该如何编写这个方法来接受一个类型参数,并返回这个类型?
采纳答案by Hyman
if you don't want to have a specific interface to handle that stuff you can use a generic method in this way:
如果你不想有一个特定的接口来处理那些东西,你可以这样使用泛型方法:
public <T> T mymethod(T type)
{
return type;
}
Mind that in this way the compiler doesn't know anything about the type that you plan to use inside that method, so you should use a bound type, for example:
请注意,通过这种方式,编译器对您计划在该方法中使用的类型一无所知,因此您应该使用绑定类型,例如:
public <T extends YourType> T mymethod(T type)
{
// now you can use YourType methods
return type;
}
But you should be sure that you need a generic method, that means that the implementation of doIt
will be the same for all the types you are planning to use it with. Otherwise if every implementation is different just overload the methods, it will work fine since return type is not used for dynamic binding:
但是你应该确定你需要一个泛型方法,这意味着doIt
你计划使用它的所有类型的实现都是相同的。否则,如果每个实现都不同,只需重载方法,它就会正常工作,因为返回类型不用于动态绑定:
public String my(String s)
{
return s;
}
public int my(int s)
{
return s;
}
int i = my(23);
String s = my("lol");
回答by duffymo
Start with this:
从这个开始:
public interface Command<T>
{
T execute(T parameter);
}
回答by TofuBeer
I would urge you to NOTuse instanceof, however this code does what you want:
我会敦促您不要使用 instanceof,但是这段代码可以满足您的需求:
public class Main
{
public static void main(String[] args)
{
final Main main;
final String strVal;
final Integer intVal;
final Float floatVal;
main = new Main();
strVal = main.doIt("Hello");
intVal = main.doIt(5);
floatVal = main.doIt(5.0f);
System.out.println(strVal);
System.out.println(intVal);
System.out.println(floatVal);
}
public <T> T doIt(final T thing)
{
T t;
if(thing instanceof String)
{
t = (T)"String";
}
else if (thing instanceof Integer)
{
t = (T)Integer.valueOf(1);
}
else
{
t = null;
}
return (t);
}
}
回答by Powerlord
The short answer is don't. As soon as instanceof
gets involved, Generics are usually the wrong answer. Use overloaded methods instead:
简短的回答是不要。一旦instanceof
涉及,泛型通常是错误的答案。改用重载方法:
public String doIt(String param){
return "string";
}
public Integer doIt(Integer param){
return 1;
}
public Object doIt(Object param){
return null;
}