您所提出的示例与Java 8无关,与Java中泛型的工作原理无关。
Function<T, Integer>function并将在编译时
Function<T, Double>function进行类型擦除,并将其转换为
Function。方法重载的经验法则是具有不同的数量,类型或参数顺序。由于这两种方法都将转换为采用
Function参数,因此编译器会抱怨它。
话虽如此,srborlongan已经提供了一种解决该问题的方法。该解决方案的问题在于,您必须
Test针对每种类型(整数,双精度等)上的每种类型的操作(加法,减法等)不断修改类。另一种解决方案是使用
methodoverriding而不是
method overloading:
Test如下更改类:
public abstract class Test<I,O extends Number> { List<I> list = new ArrayList<>(); public O performOperation(Function<I,O> function) { return list.stream().map(function).reduce((a,b)->operation(a,b)).get(); } public void add(I i) { list.add(i); } public abstract O operation(O a,O b);}创建一个子类
Test将添加两个
Integer。
public class MapStringToIntAddtionOperation extends Test<String,Integer> { @Override public Integer operation(Integer a,Integer b) { return a+b; }}然后,客户代码可以使用上述代码,如下所示:
public static void main(String []args) { Test<String,Integer> test = new MapStringToIntAddtionOperation(); test.add("1"); test.add("2"); System.out.println(test.performOperation(Integer::parseInt));}使用这种方法的优点是您的
Test班级符合该
open-closed原则。要添加新的运算(如乘法),您要做的就是添加的新子类
Test和将两个数字相乘
override的
operation方法。将其与Decorator模式结合使用,您甚至可以最小化必须创建的子类的数量。
注意 此答案中的示例仅供参考。有很多改进的领域(例如使
Test功能接口代替抽象类)超出了问题的范围。



