如何在函数 interface/lambda 调用中传递 Object 对象
How to pass Object object in functional interface/lambda call
所以,我有一段代码是这样的
public static void printStuff(Object[] stuffs, Function<?, String> func) {
for(Object stuff : stuffs) {
String stringStuff = func.apply(stuff);
System.out.println(stringStuff);
// or whatever, what is done with that String is not relevant
}
// ...
这个方法是要调用不同类型的数组,以及对应的func
值,例如:
printStuff(arrayOfClasses, (Class<?> c) -> c.getSimpleName());
printStuff(arrayOfStrings, (String s) -> '"' + s + '"');
printStuff(arrayOfObjects, o -> o.toString());
所以我绝对需要我的 stuffs 是 Object[]
,因为它是方法调用中不同类型的第一个公共超类。
在编译时,我得到:
MyClass.java:6: error: incompatible types: Object cannot be converted to CAP#1
String stringStuff = func.apply(stuff);
^
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
我的猜测是 javac 对我提供给 Function<?, String>
调用的参数发出咆哮,其类型 Object
不 extend
Object
.
所以我的问题是,如何将 Object
参数传递给 Function<?, String>
?
我可以将接口类型更改为 <Object, String>
,但它打断了我的其他调用(使用 Class[]
、String[]
等),这意味着几乎失去了全部意义通用性,不是吗?
除非有某种方法可以将我的 stuffs
类型更改为 <? extends Object>[]
之类的类型或通用类型,我很确定这是不可能的。
提前致谢各位。
编辑:
如果我将我的方法更改为通用方法,即:
public static <U> void printStuff(Object[] stuffs, Function<U, String> func) {
我仍然遇到编译错误:
MyClass.java:6: error: method apply in interface Function<T,R> cannot be applied to given types;
String stringStuff = func.apply(stuff);
^
required: U
found: Object
reason: argument mismatch; Object cannot be converted to U
一种解决方案是使用:
public static <T> void printStuff(T[] stuffs, Function<T, String> func) {
for(T stuff : stuffs) {
// ....
至于第一个代码:
public static void printStuff(Object[] stuffs, Function<?, String> func) {
for(Object stuff : stuffs) {
String stringStuff = func.apply(stuff);
System.out.println(stringStuff);
// or whatever, what is done with that String is not relevant
}
}
您遇到此错误
MyClass.java:6: error: incompatible types: Object cannot be converted to CAP#1
您会收到该错误,因为 ?
可能更具体 class,例如您还可以传递 Function<String, String>
.
类型的参数 func
你可以通过像
这样声明签名来解决这个问题
public static void printStuff(Object[] stuffs, Function<Object, String> func)
或通用方式:
public static <U> void printStuff(U[] stuffs, Function<? super U, String> func) {
for(U stuff : stuffs) {
String stringStuff = func.apply(stuff);
System.out.println(stringStuff);
// or whatever, what is done with that String is not relevant
}
}
数组的类型必须等于 Function
的第一个类型参数(或 class 的子class)。
所以,我有一段代码是这样的
public static void printStuff(Object[] stuffs, Function<?, String> func) {
for(Object stuff : stuffs) {
String stringStuff = func.apply(stuff);
System.out.println(stringStuff);
// or whatever, what is done with that String is not relevant
}
// ...
这个方法是要调用不同类型的数组,以及对应的func
值,例如:
printStuff(arrayOfClasses, (Class<?> c) -> c.getSimpleName());
printStuff(arrayOfStrings, (String s) -> '"' + s + '"');
printStuff(arrayOfObjects, o -> o.toString());
所以我绝对需要我的 stuffs 是 Object[]
,因为它是方法调用中不同类型的第一个公共超类。
在编译时,我得到:
MyClass.java:6: error: incompatible types: Object cannot be converted to CAP#1
String stringStuff = func.apply(stuff);
^
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
我的猜测是 javac 对我提供给 Function<?, String>
调用的参数发出咆哮,其类型 Object
不 extend
Object
.
所以我的问题是,如何将 Object
参数传递给 Function<?, String>
?
我可以将接口类型更改为 <Object, String>
,但它打断了我的其他调用(使用 Class[]
、String[]
等),这意味着几乎失去了全部意义通用性,不是吗?
除非有某种方法可以将我的 stuffs
类型更改为 <? extends Object>[]
之类的类型或通用类型,我很确定这是不可能的。
提前致谢各位。
编辑:如果我将我的方法更改为通用方法,即:
public static <U> void printStuff(Object[] stuffs, Function<U, String> func) {
我仍然遇到编译错误:
MyClass.java:6: error: method apply in interface Function<T,R> cannot be applied to given types;
String stringStuff = func.apply(stuff);
^
required: U
found: Object
reason: argument mismatch; Object cannot be converted to U
一种解决方案是使用:
public static <T> void printStuff(T[] stuffs, Function<T, String> func) {
for(T stuff : stuffs) {
// ....
至于第一个代码:
public static void printStuff(Object[] stuffs, Function<?, String> func) {
for(Object stuff : stuffs) {
String stringStuff = func.apply(stuff);
System.out.println(stringStuff);
// or whatever, what is done with that String is not relevant
}
}
您遇到此错误
MyClass.java:6: error: incompatible types: Object cannot be converted to CAP#1
您会收到该错误,因为 ?
可能更具体 class,例如您还可以传递 Function<String, String>
.
你可以通过像
这样声明签名来解决这个问题public static void printStuff(Object[] stuffs, Function<Object, String> func)
或通用方式:
public static <U> void printStuff(U[] stuffs, Function<? super U, String> func) {
for(U stuff : stuffs) {
String stringStuff = func.apply(stuff);
System.out.println(stringStuff);
// or whatever, what is done with that String is not relevant
}
}
数组的类型必须等于 Function
的第一个类型参数(或 class 的子class)。