Java生成的类参考 [英] Java generized class reference

查看:85
本文介绍了Java生成的类参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有签名的方法:

  Class <?扩展List< String>> getObjectType()
{
return ?????;
}

如何返回List类的正确通用版本?

  return List.class; //错误
返回List< String> .class; //错误
返回List.class< String>; //错误

正确的语法是什么?

解决方案

您需要明确地将其转换为返回类型。这样做:

  return(Class< ;? extends List< String>>)List.class; 

是的,只是看起来错了。这只是Java泛型系统混乱的原因之一。

If you have a method with the signature:

Class<? extends List<String>> getObjectType()
{
        return ?????;
}

How do you return a proper generic version of the List class?

return List.class; //errors
return List<String>.class; //errors
return List.class<String>; //errors

what is the proper syntax to handle this?

解决方案

You need to explicitly cast it to the return type. This works:

return (Class<? extends List<String>>) List.class;

Yes it just looks wrong. This is just one of the many reasons Java's generics system is a mess.

这篇关于Java生成的类参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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