类型泛型的实例 [英] Instance of type generic

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

问题描述

我的问题是这样的:

为什么不能用新的T()实例化一个泛型类型,而是用class类的newInstance()来实例化?

解决方案

您需要使用反射( newInstance()),因为在编译时,构造函数需要被链接的类是未知的。所以编译器不能生成链接。


The my question is this:

Why can not instantiate a generic type with new T () and instead with newInstance() of the class Class you can do?

解决方案

You need to use reflection (newInstance()), because at compile time the class whose constructor would need to be linked is unknown. So the compiler cannot generate the link.

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

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