English 中文(简体)
Javas 类中的 Gavas 类汇编错误汇编错误 <? 延伸 T>
原标题:Java generics compile error involving Class<? extends T>

此程序未编译 :

public class xx {
    static class Class1<C> {
        void method1(C p) {
        }
    }
    static class Class2<T> extends Class1<Class<? extends T>> {
        T object;
        void method2() {
            this.method1(this.object.getClass());
        }
    }
}

错误是 :

xx.java:10: method1(java.lang.Class<? extends T>) in xx.Class1<java.lang.Class<? extends T>>
cannot be applied to (java.lang.Class<capture#215 of ? extends java.lang.Object>)
        this.method1(this.object.getClass());

为什么发生这种情况?为什么编译者似乎相信 object. getClass () returns Class<? >/code > 延伸对象> 而不是 Class<? 扩展T> ?

最佳回答

Object.getClass () 定义为返回 Class< >? 延伸 >, 其中 T 是静态已知的接收器类型( 对象 getClass () () 调用)。 特别注意垂直条, < a href=" http://docs. oracle. com/javase/ specs/ jls/se7/ html/jls-4. html#jls-4.6" rel=“ nofol=Class < > a/ a > 。 类型变量的缩略度是其最左边的缩略范围。 在您的情况下, 隐含绑定的 Object 。 所以您要返回一个 Class<??

为什么?

想象 T = List< Integer> , 您可以不经无节制警告 , 突然执行以下 < strong> :

List<String> myStrings = new ArrayList<>();
List<Integer> myInts = new ArrayList<>();
List<Integer> myIntyStrings = myInts.getClass().cast(myStrings);
myIntyStrings.add(-1);
String myString = myStrings.get(0); // BANG!

但幸好我们确实得到了警告。 ))

问题回答

您的代码中没有设定 T 的上限, 所以 < code>? 延伸 T 确实等于 < code >? 延伸对象 。 就在昨天, 我玩了一个类似的例子, 撞上了这个屏障 。

static <T> T newInstance(T o) throws Exception {
  final Class<? extends T> c = o.getClass();
  return c.newInstance();
}

并用相同错误进行投诉。 考虑一下 : < code> Object. getClass () 的返回类型是 < code> Class<? & gt; 和编译者会想要将 < code >? 捕捉成一个具体类型。 但是, 我们不想捕捉 < code >? , 而是要“ 捕捉上捆绑” < code> T -- Java 的通用名称中没有这种内容 。

根据 getClass () 上的文档, 返回对象的类型为 < code> Class<?? 延伸 X & gt; , 其中 < code\\ X}/ code > 是该方法所调用实例类型的删除 。

因此,在类型 T 返回 Class< > 的物体上调用 getClass ()? 延伸对象 & gt; 。 在这个 API 没有关于 T 的受约束信息 。

通常使用对通用类的反射的API要求客户将类型 Classs< T> 的额外参数传递给相关的构建者或通用方法。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签