Java 8 Lambda重载
我似乎无法使它工作Java 8 Lambda重载,java,lambda,overloading,java-8,Java,Lambda,Overloading,Java 8,我似乎无法使它工作 Function<Integer, Integer> test = x -> x+x; Function<String, String> test = x -> x+x; 功能测试=x->x+x; 功能测试=x->x+x; 屈服 重复的局部变量测试 如何使test.apply(5)返回10,而test.apply(“5”)返回“55”?test是一个变量,而不是一个方法。不能重载变量 您可以尝试使用两个方法签名创建一个接口,但结果不会是
Function<Integer, Integer> test = x -> x+x;
Function<String, String> test = x -> x+x;
功能测试=x->x+x;
功能测试=x->x+x;
屈服
重复的局部变量测试
如何使
test.apply(5)
返回10,而test.apply(“5”)
返回“55”?test
是一个变量,而不是一个方法。不能重载变量
您可以尝试使用两个方法签名创建一个接口,但结果不会是一个函数接口,也无法使用lambda实现它。因此,我们再次失败。
测试是一个变量,而不是一种方法。不能重载变量
您可以尝试使用两个方法签名创建一个接口,但结果不会是一个函数接口,也无法使用lambda实现它。因此,我们再次失败。简单地说:你不能
你所做的不是超载。它创建了两个名称冲突的函数
接口的独立实例
这相当于做:
int a = 1;
int a = 2;
在同样的范围内,同样非法
如果在函数
接口中有两个应用
定义,则会发生重载-一个接受字符串
参数,另一个接受int
。您不能(轻松地)修改java中的现有接口。简单地说:您不能
你所做的不是超载。它创建了两个名称冲突的函数
接口的独立实例
这相当于做:
int a = 1;
int a = 2;
在同样的范围内,同样非法
如果在函数
接口中有两个应用
定义,则会发生重载-一个接受字符串
参数,另一个接受int
。您不能(轻松地)修改java中的现有接口。您不能拥有一个匿名函数的测试变量,该匿名函数同时实现函数
和函数
(顺便说一句,这是同一个接口)
但是,您可以使用重载的apply
方法创建一个嵌套类,并最终使用lambda函数构造它。这个嵌套类不需要(也不能)实现函数
和函数
。但如以下示例所示,您实际上不需要实现这些接口:
static class FunctionOverload {
private final Function<String, String> stringFunction;
private final Function<Integer, Integer> integerFunction;
FunctionOverload(Function<String, String> stringFunction, Function<Integer, Integer> integerFunction) {
this.stringFunction = stringFunction;
this.integerFunction = integerFunction;
}
public String apply(String string) {
return this.stringFunction.apply(string);
}
public Integer apply(Integer integer) {
return this.integerFunction.apply(integer);
}
}
public static void main(String[] args) {
FunctionOverload test = new FunctionOverload(x -> x + x, x -> x + x);
test.apply("5");
test.apply(5);
Stream.of("5").map(test::apply);
Stream.of(5).map(test::apply);
}
静态类函数重载{
私人最终功能;
私有最终函数整数函数;
函数重载(函数stringFunction、函数integerFunction){
this.stringFunction=stringFunction;
this.integerFunction=integerFunction;
}
公共字符串应用(字符串){
返回此.stringFunction.apply(字符串);
}
公共整数应用(整数){
返回此.integerFunction.apply(整数);
}
}
公共静态void main(字符串[]args){
函数重载测试=新函数重载(x->x+x,x->x+x);
测试。应用(“5”);
试验。应用(5);
流程图(测试::应用);
流(5.map)(测试::应用);
}
您不能有一个匿名函数的测试
变量,该匿名函数同时实现函数
和函数
(顺便说一句,它是同一个接口)
但是,您可以使用重载的apply
方法创建一个嵌套类,并最终使用lambda函数构造它。这个嵌套类不需要(也不能)同时实现函数
和函数
。但如以下示例所示,您实际上不需要实现这些接口:
static class FunctionOverload {
private final Function<String, String> stringFunction;
private final Function<Integer, Integer> integerFunction;
FunctionOverload(Function<String, String> stringFunction, Function<Integer, Integer> integerFunction) {
this.stringFunction = stringFunction;
this.integerFunction = integerFunction;
}
public String apply(String string) {
return this.stringFunction.apply(string);
}
public Integer apply(Integer integer) {
return this.integerFunction.apply(integer);
}
}
public static void main(String[] args) {
FunctionOverload test = new FunctionOverload(x -> x + x, x -> x + x);
test.apply("5");
test.apply(5);
Stream.of("5").map(test::apply);
Stream.of(5).map(test::apply);
}
静态类函数重载{
私人最终功能;
私有最终函数整数函数;
函数重载(函数stringFunction、函数integerFunction){
this.stringFunction=stringFunction;
this.integerFunction=integerFunction;
}
公共字符串应用(字符串){
返回此.stringFunction.apply(字符串);
}
公共整数应用(整数){
返回此.integerFunction.apply(整数);
}
}
公共静态void main(字符串[]args){
函数重载测试=新函数重载(x->x+x,x->x+x);
测试。应用(“5”);
试验。应用(5);
流程图(测试::应用);
流(5.map)(测试::应用);
}
尽管上面的代码试图“重载变量”时出现了问题,但这是可以做到的。一个快速但丑陋的解决方案如下:
@SuppressWarnings("unchecked")
public static <T> T addOrConcat(T x){
if (x instanceof String){
String ret = x.toString();
return (T)(ret + ret);
} else if (x instanceof Integer){
Integer ret = (Integer)x + (Integer)x;
return (T)(ret);
}
return null;
}
public static void main(String[] args) {
UnaryOperator<Object> test = x -> addOrConcat(x);
System.out.println(test.apply(Integer.valueOf(5)));
System.out.println(test.apply("5"));
}
@SuppressWarnings(“未选中”)
公共静态T addOrConcat(T x){
if(字符串的x实例){
字符串ret=x.toString();
返回(T)(ret+ret);
}else if(整数的x实例){
整数ret=(整数)x+(整数)x;
返回(T)(ret);
}
返回null;
}
公共静态void main(字符串[]args){
一元运算符测试=x->addOrConcat(x);
System.out.println(test.apply(Integer.valueOf(5));
系统输出打印LN(测试应用(“5”));
}
产出如下:
@SuppressWarnings("unchecked")
public static <T> T addOrConcat(T x){
if (x instanceof String){
String ret = x.toString();
return (T)(ret + ret);
} else if (x instanceof Integer){
Integer ret = (Integer)x + (Integer)x;
return (T)(ret);
}
return null;
}
public static void main(String[] args) {
UnaryOperator<Object> test = x -> addOrConcat(x);
System.out.println(test.apply(Integer.valueOf(5)));
System.out.println(test.apply("5"));
}
十,
五十五
如果有时间的话,我想我们可以想出一些更有效、更安全、更直观的方法。尽管上面的代码有问题,但尝试“重载变量”是可以做到的。一个快速但丑陋的解决方案如下:
@SuppressWarnings("unchecked")
public static <T> T addOrConcat(T x){
if (x instanceof String){
String ret = x.toString();
return (T)(ret + ret);
} else if (x instanceof Integer){
Integer ret = (Integer)x + (Integer)x;
return (T)(ret);
}
return null;
}
public static void main(String[] args) {
UnaryOperator<Object> test = x -> addOrConcat(x);
System.out.println(test.apply(Integer.valueOf(5)));
System.out.println(test.apply("5"));
}
@SuppressWarnings(“未选中”)
公共静态T addOrConcat(T x){
if(字符串的x实例){
字符串ret=x.toString();
返回(T)(ret+ret);
}else if(整数的x实例){
整数ret=(整数)x+(整数)x;
返回(T)(ret);
}
返回null;
}
公共静态void main(字符串[]args){
一元运算符测试=x->addOrConcat(x);
System.out.println(test.apply(Integer.valueOf(5));
系统输出打印LN(测试应用(“5”));
}
产出如下:
@SuppressWarnings("unchecked")
public static <T> T addOrConcat(T x){
if (x instanceof String){
String ret = x.toString();
return (T)(ret + ret);
} else if (x instanceof Integer){
Integer ret = (Integer)x + (Integer)x;
return (T)(ret);
}
return null;
}
public static void main(String[] args) {
UnaryOperator<Object> test = x -> addOrConcat(x);
System.out.println(test.apply(Integer.valueOf(5)));
System.out.println(test.apply("5"));
}
十,
五十五
如果有时间,我想我们可以想出更有效的办法,s