英文:
Java PECS cannot add to consumer
问题
import java.util.*;
public class Test
{
public static void main(String[] args)
{
List<Integer> integers = new ArrayList<>();
test(integers);
}
public static <T> void test(List<? super T> to)
{
to.add(32);
}
}
根据PECS原则(生产者使用extends,消费者使用super),我使用了super,但是出现了这个错误:
Test.java:13: error: no suitable method found for add(int)
to.add(32);
^
method Collection.add(CAP#1) is not applicable
(argument mismatch; int cannot be converted to CAP#1)
method List.add(CAP#1) is not applicable
(argument mismatch; int cannot be converted to CAP#1)
where T is a type-variable:
T extends Object declared in method <T>test(List<? super T>)
where CAP#1 is a fresh type-variable:
CAP#1 extends Object super: T from capture of ? super T
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
英文:
Why can I not add an integer to the consumer?
import java.util.*;
public class Test
{
public static void main(String[] args)
{
List<Integer> integers = new ArrayList<>();
test(integers);
}
public static <T> void test(List<? super T> to)
{
to.add(32);
}
}
According to PECS (Producer extends, Consumer super), I use super, but this error occurs:
Test.java:13: error: no suitable method found for add(int)
to.add(32);
^
method Collection.add(CAP#1) is not applicable
(argument mismatch; int cannot be converted to CAP#1)
method List.add(CAP#1) is not applicable
(argument mismatch; int cannot be converted to CAP#1)
where T is a type-variable:
T extends Object declared in method <T>test(List<? super T>)
where CAP#1 is a fresh type-variable:
CAP#1 extends Object super: T from capture of ? super T
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
答案1
得分: 2
简单回答:因为在你的示例中,没有 T 类型与 int 类型之间的关系。
然而,它将按照以下方式工作:
```java
public class Test
{
public static void main(String[] args)
{
List<? super Integer> integers = new ArrayList<>();
test(integers, 32);
}
public static <T> void test(List<? super T> to, T elem)
{
to.add(elem);
}
}
还可以按照以下方式工作:
public class Test
{
public static void main(String[] args)
{
List<? super Integer> integers = new ArrayList<>();
test(integers);
}
public static void test(List<? super Integer> to)
{
to.add(32);
}
}
原因是你需要“解释”编译器集合类型与元素类型之间的关系。
附:在这里阅读更多信息 1
<details>
<summary>英文:</summary>
Simple answer: because there's no T relation to int in your example.
It will work like this however:
public class Test
{
public static void main(String[] args)
{
List<Integer> integers = new ArrayList<>();
test(integers, 32);
}
public static <T> void test(List<? super T> to, T elem)
{
to.add(elem);
}
}
And also like this:
public class Test
{
public static void main(String[] args)
{
List<Integer> integers = new ArrayList<>();
test(integers);
}
public static void test(List<? super Integer> to)
{
to.add(32);
}
}
The reason is you need to "explain" compiler how is your collection type relate to the element type.
PS have a read [here][1]
[1]: https://howtodoinjava.com/java/generics/java-generics-what-is-pecs-producer-extends-consumer-super/
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论