类别名称< T>泛型类 [英] Name of Class <T> of a generic Class

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

问题描述

在使用泛型时,我在java中遇到了一个小问题。我有一个班级A:

  public class A< T> 

在A的方法中,我需要获取类型名称T.是否有办法如果我创建 A< String> temp = new A< String>();

$ c>,我希望能够在某一点获得java.lang.String - 我必须使用泛型,因为我的一个方法将不得不返回一个List。)



非常感谢您的帮助。

解决方案

由于 type erasure ,这是不可能的。



相反,您需要接受 Type< T> 在你的构造函数中。


I have a small problem in java while using generics. I have a class A :

public class A<T>

In a method of A, I need to get the type name of T. Is there a way to find the string s using T ?

(If I create A<String> temp = new A<String>();, I want to be able to get java.lang.String at one point - I have to use generics because one of my methods will have to return a List).

This seems quite easy but I do not see how to do it.

Thanks a lot in advance for your help.

解决方案

Due to type erasure, this is not possible.

Instead, you need to accept a Type<T> in your constructor.

这篇关于类别名称&lt; T&gt;泛型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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