我正在尝试使用单个方法(当然)在 Java 中创建一个功能接口,该方法可以采用任何类型的参数并返回任何数据类型(即泛型方法)。
这是我到目前为止:
计算器.java
public interface Calculator<T> {
T operation(T n1, T... n2); //this is where the optional parameter comes in
}
Run Code Online (Sandbox Code Playgroud)
主程序
public static void main(String[] args) {
Calculator<Integer> addition = (n1, n2) -> n1 + n2; //this gives an error
}
Run Code Online (Sandbox Code Playgroud)
错误说:
二元运算符“+”的错误操作数类型