Java中的PECS无法添加到消费者。

huangapple go评论104阅读模式
英文:

Java PECS cannot add to consumer

问题

  1. import java.util.*;
  2. public class Test
  3. {
  4. public static void main(String[] args)
  5. {
  6. List<Integer> integers = new ArrayList<>();
  7. test(integers);
  8. }
  9. public static <T> void test(List<? super T> to)
  10. {
  11. to.add(32);
  12. }
  13. }

根据PECS原则(生产者使用extends,消费者使用super),我使用了super,但是出现了这个错误:

  1. Test.java:13: error: no suitable method found for add(int)
  2. to.add(32);
  3. ^
  4. method Collection.add(CAP#1) is not applicable
  5. (argument mismatch; int cannot be converted to CAP#1)
  6. method List.add(CAP#1) is not applicable
  7. (argument mismatch; int cannot be converted to CAP#1)
  8. where T is a type-variable:
  9. T extends Object declared in method <T>test(List<? super T>)
  10. where CAP#1 is a fresh type-variable:
  11. CAP#1 extends Object super: T from capture of ? super T
  12. Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
  13. 1 error
英文:

Why can I not add an integer to the consumer?

  1. import java.util.*;
  2. public class Test
  3. {
  4. public static void main(String[] args)
  5. {
  6. List&lt;Integer&gt; integers = new ArrayList&lt;&gt;();
  7. test(integers);
  8. }
  9. public static &lt;T&gt; void test(List&lt;? super T&gt; to)
  10. {
  11. to.add(32);
  12. }
  13. }

According to PECS (Producer extends, Consumer super), I use super, but this error occurs:

  1. Test.java:13: error: no suitable method found for add(int)
  2. to.add(32);
  3. ^
  4. method Collection.add(CAP#1) is not applicable
  5. (argument mismatch; int cannot be converted to CAP#1)
  6. method List.add(CAP#1) is not applicable
  7. (argument mismatch; int cannot be converted to CAP#1)
  8. where T is a type-variable:
  9. T extends Object declared in method &lt;T&gt;test(List&lt;? super T&gt;)
  10. where CAP#1 is a fresh type-variable:
  11. CAP#1 extends Object super: T from capture of ? super T
  12. Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
  13. 1 error

答案1

得分: 2

  1. 简单回答:因为在你的示例中,没有 T 类型与 int 类型之间的关系。
  2. 然而,它将按照以下方式工作:
  3. ```java
  4. public class Test
  5. {
  6. public static void main(String[] args)
  7. {
  8. List<? super Integer> integers = new ArrayList<>();
  9. test(integers, 32);
  10. }
  11. public static <T> void test(List<? super T> to, T elem)
  12. {
  13. to.add(elem);
  14. }
  15. }

还可以按照以下方式工作:

  1. public class Test
  2. {
  3. public static void main(String[] args)
  4. {
  5. List<? super Integer> integers = new ArrayList<>();
  6. test(integers);
  7. }
  8. public static void test(List<? super Integer> to)
  9. {
  10. to.add(32);
  11. }
  12. }

原因是你需要“解释”编译器集合类型与元素类型之间的关系。

附:在这里阅读更多信息 1

  1. <details>
  2. <summary>英文:</summary>
  3. Simple answer: because there&#39;s no T relation to int in your example.
  4. It will work like this however:
  5. public class Test
  6. {
  7. public static void main(String[] args)
  8. {
  9. List&lt;Integer&gt; integers = new ArrayList&lt;&gt;();
  10. test(integers, 32);
  11. }
  12. public static &lt;T&gt; void test(List&lt;? super T&gt; to, T elem)
  13. {
  14. to.add(elem);
  15. }
  16. }
  17. And also like this:
  18. public class Test
  19. {
  20. public static void main(String[] args)
  21. {
  22. List&lt;Integer&gt; integers = new ArrayList&lt;&gt;();
  23. test(integers);
  24. }
  25. public static void test(List&lt;? super Integer&gt; to)
  26. {
  27. to.add(32);
  28. }
  29. }
  30. The reason is you need to &quot;explain&quot; compiler how is your collection type relate to the element type.
  31. PS have a read [here][1]
  32. [1]: https://howtodoinjava.com/java/generics/java-generics-what-is-pecs-producer-extends-consumer-super/
  33. </details>

huangapple
  • 本文由 发表于 2020年4月9日 22:53:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61124009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定