英文:
Confused with Java wildcards
问题
我对Java泛型很陌生,感到非常困惑。
我编写了这些代码,但是遇到了错误:"所需类型:? extends Number 的捕获,实际提供:Integer"。
出了什么问题?
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class TestGeneric {
// 我想创建一个Function列表,这些Function的输入/输出类型可以彼此不同。
private List<Function<? extends Number, ? extends Number>> functions;
public TestGeneric() {
functions = new ArrayList<>();
}
public void addFunction(Function<? extends Number, ? extends Number> function) {
functions.add(function);
}
public static void main(String[] args) {
TestGeneric testGeneric = new TestGeneric();
Function<Integer, Integer> hundredTimes = new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
return integer * 100;
}
};
testGeneric.addFunction(hundredTimes);
Function<Double, Double> tenTimes = new Function<Double, Double>() {
@Override
public Double apply(Double o) {
return 10.0 * o;
}
};
testGeneric.addFunction(tenTimes);
System.out.println(testGeneric.functions.get(0).apply(Integer.valueOf(10)));
}
}
英文:
I am new to Java generics and I am so confused.
I wrote this piece of codes, but I got error "Required type:
capture of ? extends Number, Provided: Integer".
What went wrong?
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class TestGeneric {
// I want to create a list of Functions, and these Functions can have different input/output types than one another.
private List<Function<? extends Number, ? extends Number>> functions;
public TestGeneric() {
functions = new ArrayList<>();
}
public void addFunction(Function<? extends Number, ? extends Number> function) {
functions.add(function);
}
public void main(String[] args) {
TestGeneric testGeneric = new TestGeneric();
Function<Integer, Integer> hundredTimes = new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
return integer * 100;
}
};
functions.add(hundredTimes);
Function<Double, Double> tenTimes = new Function<Double, Double>() {
@Override
public Double apply(Double o) {
return 10.0 * o;
}
};
functions.add(tenTimes);
System.out.println(functions.get(0).apply(Integer.valueOf(10)));
}
}
答案1
得分: 1
主要问题是您的函数列表的定义:
private List<Function<? extends Number, ? extends Number>> functions;
我建议您尝试针对您的情况使用:
private List<Function<Number, ? extends Number>> functions;
然后稍微修改其他函数...
Function<Number, Integer> hundredTimes = integer -> integer.intValue() * 100;
functions.add(hundredTimes);
Function<Number, Double> tenTimes = o -> 10.0 * o.doubleValue();
functions.add(tenTimes);
System.out.println(functions.get(0).apply(Integer.valueOf(10)));
使用Lambda表达式,您最终可以编写:
functions.add(n -> n.intValue() * 100);
functions.add(n -> 10.0 * n.doubleValue());
System.out.println(functions.get(0).apply(10));
问题在于通配符的含义与您最初的想法不同。使用通配符意味着您使用了特定的类型,但您不知道它是什么。在您要将10添加为整数时,函数要求特定的类型,而这个类型是?
。因此,这样是行不通的。也许在方法或类上使用泛型,根据用例,也可以解决您的问题。但是更改为Number
应该能解决问题。
英文:
The main problem is the definition of your functions list
private List<Function<? extends Number, ? extends Number>> functions;
I would try to use for your case:
private List<Function<Number, ? extends Number>> functions;
Then modify the other functions a little bit...
Function<Number, Integer> hundredTimes = new Function<Number, Integer>() {
@Override
public Integer apply(Number integer) {
return integer.intValue() * 100;
}
};
functions.add(hundredTimes);
Function<Number, Double> tenTimes = new Function<Number, Double>() {
@Override
public Double apply(Number o) {
return 10.0 * o.doubleValue();
}
};
functions.add(tenTimes);
System.out.println(functions.get(0).apply(Integer.valueOf(10)));
Using lambdas you could finally write:
functions.add( n -> n.intValue() * 100);
functions.add(n -> 10.0 * n.doubleValue());
System.out.println(functions.get(0).apply(10));
The problem is that the wildcard has an other meaning than you think by first thoughts. Using a wildcard means you use a specific type but you dont know it. At the time you want to add 10 as an int, the function asks for the specific type which is ?. So this will not work. Perhaps using generics on methods or on the class could solve your problem also depending on the use case. But change to Number should solve the problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论