是否可以在不使 class 通用于 return List<T> 的情况下使方法通用
Is it possible to make method generic without making class generic to return List<T>
是否可以在不使 class 通用的情况下使方法通用?
无论如何使用通用类型可以实现以下目标吗?
下面的代码无法编译,这只是为了展示我想要实现的目标。
public class Test {
public List<T> execute(String name, Map<String, Object> params, List<T> type) {
List<Object> emps = new ArrayList<Object>();
emps.add(new Employee());
//emps object will be generated dynamically
return (List<T>)emps;
}
public static void main(String[] args) {
Test test = new Test();
test.execute("testing", null, new ArrayList<Employee>());
}
}
试试这个:
public <T> List<T> execute(String name, Map<String, Object> params, List<T> type) {
List<Object> emps = new ArrayList<Object>();
emps.add(new Employee());
//emps object will be generated dynamically
return (List<T>) emps;
}
看看这个解释:
The Java compiler takes advantage of target typing to infer the type
parameters of a generic method invocation. The target type of an
expression is the data type that the Java compiler expects depending
on where the expression appears. Consider the method
Collections.emptyList
, which is declared as follows:
static <T> List<T> emptyList();
Consider the following assignment
statement:
List<String> listOne = Collections.emptyList();
This statement is
expecting an instance of List<String>;
this data type is the target
type. Because the method emptyList
returns a value of type List<T>
,
the compiler infers that the type argument T must be the value String.
This works in both Java SE 7 and 8. Alternatively, you could use a
type witness and specify the value of T as follows:
List<String> listOne = Collections.<String>emptyList();
https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html
是的,您可以在非泛型 class 中通过使用 <T>
表示法将其声明为泛型来将方法定义为泛型。
简单地将方法签名更改为:
public <T> List<T> execute(String name, Map<String, Object> params, List<T> type)
并且该方法将是 return 类型为 T
的通用 List
对象。
尝试下面它应该工作
public List<?> execute(String name, Map<String, Object> params, List<?> type) {
}
是否可以在不使 class 通用的情况下使方法通用? 无论如何使用通用类型可以实现以下目标吗?
下面的代码无法编译,这只是为了展示我想要实现的目标。
public class Test {
public List<T> execute(String name, Map<String, Object> params, List<T> type) {
List<Object> emps = new ArrayList<Object>();
emps.add(new Employee());
//emps object will be generated dynamically
return (List<T>)emps;
}
public static void main(String[] args) {
Test test = new Test();
test.execute("testing", null, new ArrayList<Employee>());
}
}
试试这个:
public <T> List<T> execute(String name, Map<String, Object> params, List<T> type) {
List<Object> emps = new ArrayList<Object>();
emps.add(new Employee());
//emps object will be generated dynamically
return (List<T>) emps;
}
看看这个解释:
The Java compiler takes advantage of target typing to infer the type parameters of a generic method invocation. The target type of an expression is the data type that the Java compiler expects depending on where the expression appears. Consider the method
Collections.emptyList
, which is declared as follows:
static <T> List<T> emptyList();
Consider the following assignment statement:
List<String> listOne = Collections.emptyList();
This statement is expecting an instance ofList<String>;
this data type is the target type. Because the methodemptyList
returns a value of typeList<T>
, the compiler infers that the type argument T must be the value String. This works in both Java SE 7 and 8. Alternatively, you could use a type witness and specify the value of T as follows:
List<String> listOne = Collections.<String>emptyList();
https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html
是的,您可以在非泛型 class 中通过使用 <T>
表示法将其声明为泛型来将方法定义为泛型。
简单地将方法签名更改为:
public <T> List<T> execute(String name, Map<String, Object> params, List<T> type)
并且该方法将是 return 类型为 T
的通用 List
对象。
尝试下面它应该工作
public List<?> execute(String name, Map<String, Object> params, List<?> type) {
}