java泛型得到T.class

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/buyaore_wo/article/details/8138846
import java.lang.reflect.ParameterizedType;

public class Test<T> {
	public Class<T> getTClass() {
		return (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
	}

	public void test() {
		System.out.println(getTClass());
	}

	public static void main(String s[]) {
		(new Test<String>() {
		}).test();
	}

}

输出结果:

class java.lang.String

猜你喜欢

转载自blog.csdn.net/buyaore_wo/article/details/8138846