gpt4 book ai didi

Java 泛型编译器错误 : incompatible types

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:15:41 24 4
gpt4 key购买 nike

当用 Java 做一些不太特别的事情时,我遇到了一个泛型错误,我无法理解它为什么不起作用。代码是:

package test;
import java.util.*;
public class TestClass {
public static class A extends C{}
public static class B extends C{}
public static class C{}
public static class D<T>{}
public static class E<T>{}

public static void main(String args[]){
E<D<? extends C>> a = new E<D<A>>();
E<D<? extends Object>> b = new E<D<? extends C>>();
E<D<? extends A>> c = new E<D<A>>();
E<D<? super A>> d = new E<D<A>>();
D<? extends C> e = new D<A>();
D<? extends A> f = new D<A>();
D<? extends A> g = new D<A>();
}
}

编译时出现的错误是:

test/TestClass.java:11: incompatible typesfound   : test.TestClass.E<test.TestClass.D<test.TestClass.A>>required: test.TestClass.E<test.TestClass.D<? extends test.TestClass.C>>      E<D<? extends C>> a = new E<D<A>>();                            ^test/TestClass.java:12: incompatible typesfound   : test.TestClass.E<test.TestClass.D<? extends test.TestClass.C>>required: test.TestClass.E<test.TestClass.D<? extends java.lang.Object>>      E<D<? extends Object>> b = new E<D<? extends C>>();                                 ^test/TestClass.java:13: incompatible typesfound   : test.TestClass.E<test.TestClass.D<test.TestClass.A>>required: test.TestClass.E<test.TestClass.D<? extends test.TestClass.A>>      E<D<? extends A>> c = new E<D<A>>();                            ^test/TestClass.java:14: incompatible typesfound   : test.TestClass.E<test.TestClass.D<test.TestClass.A>>required: test.TestClass.E<test.TestClass.D<? super test.TestClass.A>>      E<D<? super A>> d = new E<D<A>>();                          ^4 errors

如果E<D<? extends C>>找到了,那肯定匹配 E<D<? extends Object>> , 正确的?还是我错过了什么?

最佳答案

也许这会帮助你理解:

    ArrayList<Object> aList = new ArrayList<String>();

这不会出现类似的错误。

编辑:向咨询: http://www.ibm.com/developerworks/java/library/j-jtp01255.html

关于Java 泛型编译器错误 : incompatible types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1276797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com