Java的:T已OBJ; obj.getClass()的类型是类<>而不是类&LT ;?扩展T&取代。为什么? [英] Java: T obj; type of obj.getClass() is Class<?> and not Class<? extends T>. why?

查看:132
本文介绍了Java的:T已OBJ; obj.getClass()的类型是类<>而不是类&LT ;?扩展T&取代。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这样的功能:

<T> void foo(T obj)

类型 obj.getClass()类和LT的;?&GT; ,而不是类和LT ;?扩展T&GT; 。为什么呢?

The type of obj.getClass() is Class<?> and not Class<? extends T>. Why?

以下code正常工作:

The following code works fine:

String foo = "";
Class<? extends String> fooClass = foo.getClass();

所以签名 T#的getClass()似乎返回类和LT ;?扩展T&GT; ,对

为什么签名不同,如果 T 真的是一个通用的?

Why is the signature different if T really is a generic?

要解决这个问题(并使其更清楚我闲逛),我已经实现了这个功能:

To overcome the problem (and to make it more clear what I wander about), I have implemented this function:

@SuppressWarnings("unchecked") static <T> Class<? extends T> classOf(T obj) {
    return (Class<? extends T>) obj.getClass();
}

问题又来了:为什么投这里,而不是需要在字符串情况?以及为什么是
燮pressWarnings 需要的?是不是总是从code明确表示,将始终能够安全地做到这一点投?

Again the question: Why is the cast needed here and not in the String case? And why is the SuppressWarnings needed? Isn't it always clear from the code that it will always be able to safely do this cast?

有什么办法,我可以得到一个类和LT ;?扩展T&GT; OBJ ?如果是的话,怎么样?如果不是,为什么不呢?

Is there any way I can get a Class<? extends T> from obj? If yes, how? If not, why not?

一种方法是使用 classOf 。这将是安全的,对吗?如果总是安全并给出了一个安全的方式来真正获得类和LT ;?扩展T&GT; (而不是类&LT;&GT; ),为什么会出现在Java中没有这样的功能呢?还是有?

One way would be to use classOf. That would be safe, right? If that is always safe and gives a safe way to really get a Class<? extends T> (instead of a Class<?>), why is there no such function in Java? Or is there?

怎么样案例:

<T> void bar(T[] array)

array.getClass()getComponentType()再次返回类&LT;?&GT; ,而不是类和LT ;?扩展T&GT; 。为什么呢?

array.getClass().getComponentType() again returns a Class<?> and not a Class<? extends T>. Why?

我已经实现这个功能:

@SuppressWarnings("unchecked") static <T> Class<? extends T> classOf(T[] array) {
    return (Class<? extends T>) array.getClass().getComponentType();
}

这是再安全使用?

Is this again safe to use?

要澄清更多的是什么我不知道的。考虑这个演示code:

To clarify more what I wonder about. Consider this demo code:

static interface I<T> {
    Class<? extends T> myClass();
}

static class A implements I<A> {
    public Class<? extends A> myClass() {
        return this.getClass();
    }
}

static <T> void foo(I<T> obj) {
    Class<? extends T> clazz = obj.myClass(); // this works
}

这工作正常。但是,同样不适合对象#的getClass()

This works fine. But the same does not for Object#getClass().

为什么没有可能,例如,有一个像 ClassInstance的&LT通用接口; T&GT; 使用功能的getClass(),并自动执行这一每一个Java对象?这将有完全相同的,我在解决谈到改善有它从一个非通用基础类对象扩展

Why wasn't it possible for example to have a generic interface like ClassInstance<T> with the function getClass() and every Java Object automatically implementing this? This would have exactly those improvements I am talking about over the solution to have it extending from a non-generic base class Object.

或有对象作为一个泛型类:

static abstract class Object<T> {
    abstract Class<? extends T> myClass();
}

static class B extends Object<B> {
    public Class<? extends B> myClass() {
        return this.getClass();
    }
}

static <T> void bar(Object<T> obj) {
    Class<? extends T> clazz = obj.myClass(); // this works
}

现在想起 MyClass的()的getClass()想想吧,编译器会自动添加到每类。这将已经解决了很多的问题,铸造

Now think of myClass() as getClass() and think about that the compiler would automatically add that to every class. It would have resolved a lot of those casting issues.

我谈论的主要问题是:为什么是不是犯了这样的

The main question I am talking about is: Why wasn't it made like this?

或者重新把它放在不同的词:在这里,我更多的描述详细克服了这样的问题 classOf 功能的解决方案。的为什么不是犯了这样的,即为什么原有的功能不是这样?

Or to put it again in different words: Here, I describe in more detail the solution of such classOf function which overcomes the problem. Why wasn't it made like this, i.e. why is the original function not like this?

(我真的不希望得到一个答案,如:从一个非一般的对象扩展Java的方式工作的权利,即定义此功能,使这是不可能的。我问为什么它不以某种方式解决不同的,这样它本来可能的。)

(I don't really want to get an answer like: the way Java works right now, i.e. extending from a non-generic Object which defines this function, makes this not possible. I am asking why it wasn't solved somehow differently so that it would have been possible.)

推荐答案

最根本的问题是,的getClass()不返回,因为它在对象级别定义的类。即,它被mearly定义为延伸对象的类。他们可以像定义的getClass()。

The basic problem is that getClass() doesn't return the class because its defined at the Object level. i.e. it is mearly defined as a class which extends object. They could have defined getClass() like.

Class<this> getClass() { /**/ }

而是它

Class<?> getClass()

这意味着仿制药没有什么收益的getclass理解。

which means generics has no understanding of what getClass returns.

这篇关于Java的:T已OBJ; obj.getClass()的类型是类&LT;&GT;而不是类&LT ;?扩展T&取代。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆