我想使用一个使用泛型参数的方法,并在类层次结构上返回泛型结果.
编辑:没有 SupressWarnings("未选中")答案允许:-)
这是一个说明我的问题的示例代码:
import java.util.*;
public class GenericQuestion {
interface Function<F, R> {R apply(F data);}
static class Fruit {int id; String name; Fruit(int id, String name) {
this.id = id; this.name = name;}
}
static class Apple extends Fruit {
Apple(int id, String type) { super(id, type); }
}
static class Pear extends Fruit {
Pear(int id, String type) { super(id, type); }
}
public static void main(String[] args) {
List<Apple> apples = Arrays.asList(
new Apple(1,"Green"), new …
Run Code Online (Sandbox Code Playgroud)