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

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

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&lt;Integer&gt; integers = new ArrayList&lt;&gt;();
        test(integers);
    }
    
    public static &lt;T&gt; void test(List&lt;? super T&gt; 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 &lt;T&gt;test(List&lt;? super T&gt;)
  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&#39;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&lt;Integer&gt; integers = new ArrayList&lt;&gt;();
            test(integers, 32);
        }
    
        public static &lt;T&gt; void test(List&lt;? super T&gt; to, T elem)
        {
            to.add(elem);
        }
    }

And also like this:

    public class Test
    {
        public static void main(String[] args)
        {
            List&lt;Integer&gt; integers = new ArrayList&lt;&gt;();
            test(integers);
        }
    
        public static void test(List&lt;? super Integer&gt; to)
        {
            to.add(32);
        }
    }

The reason is you need to &quot;explain&quot; 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>



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:

确定