日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

[转载] Java获取泛型T的类型 T.class

發布時間:2025/3/11 java 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载] Java获取泛型T的类型 T.class 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考鏈接: Java中的抽象

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;

?

public class Main{

? ? public static void main(String[] args)

? ? {

? ? ? ? Foo<String> foo = new Foo<String>(){};

? ? ? ? // 在類的外部這樣獲取

? ? ? ? Type type = ((ParameterizedType)foo.getClass().getGenericSuperclass()).getActualTypeArguments()[0];

? ? ? ? System.out.println(type);

? ? ? ? // 在類的內部這樣獲取

? ? ? ? System.out.println(foo.getTClass());

? ? }

}

?

abstract class Foo<T>{

? ? public Class<T> getTClass()

? ? {

? ? ? ? Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];

? ? ? ? return tClass;

? ? }

}?

輸出:?

class java.lang.String

class java.lang.String?

上面的代碼不是萬能的,只有實例化T的子類才能按上述方法獲得T的實際類型,? 如果子類沒有實例化T,則無法獲取T的實際類型;? 比如,class Child 并沒有實例化T,所以得不到String.class;?

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;

?

public class Main{

? ? public static void main(String[] args){

? ? ? ? //區別在new Child<String>()沒有{}匿名類

? ? ? ? Foo<String> foo = new Child<String>();

? ? ? ? // 在類的外部這樣獲取

? ? ? ? Type type = ((ParameterizedType)foo.getClass().getGenericSuperclass()).getActualTypeArguments()[0];

? ? ? ? System.out.println(type);

? ? ? ? // 在類的內部這樣獲取

? ? ? ? System.out.println(foo.getTClass());

? ? }

}

?

abstract class Foo<T>{

? ? public Class<T> getTClass()

? ? {

? ? ? ? Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];

? ? ? ? return tClass;

? ? }

}

?

class Child<T> extends Foo<T>{

}?

輸出:?

Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

? ? at com.hankcs.Main.main(Main.java:9)

? ? at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

? ? at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

? ? at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

? ? at java.lang.reflect.Method.invoke(Method.java:606)?

有一種解決方式,父類本身不獲取泛型的具體類型,僅提供抽象方法,由子類來提供具體的類型?

public abstract class Foo<T> {??

? ? public abstract Class getEntityClass();??

}??

?

public class Child extends Foo<String> {??

? ? public Class getEntityClass() {??

? ? ? ? return String.class;??

? ? }??

}? ?

對于獲取泛型的方法,比較完整的代碼如下:?

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;

?

public class GenericsUtils {

? ? /**

? ? ?* 通過反射,獲得定義Class時聲明的父類的范型參數的類型. 如public BookManager extends

? ? ?* GenricManager<Book>

? ? ?*?

? ? ?* @param clazz The class to introspect

? ? ?* @return the first generic declaration, or <code>Object.class</code> if cannot be determined

? ? ?*/

? ? public static Class getSuperClassGenricType(Class clazz) {

? ? ? ? return getSuperClassGenricType(clazz, 0);

? ? }

?

? ? /**

? ? ?* 通過反射,獲得定義Class時聲明的父類的范型參數的類型. 如public BookManager extends GenricManager<Book>

? ? ?*?

? ? ?* @param clazz clazz The class to introspect

? ? ?* @param index the Index of the generic ddeclaration,start from 0.

? ? ?*/

? ? public static Class getSuperClassGenricType(Class clazz, int index)

? ? ? ? ? ? throws IndexOutOfBoundsException {

? ? ? ? Type genType = clazz.getGenericSuperclass();

? ? ? ? if (!(genType instanceof ParameterizedType)) {

? ? ? ? ? ? return Object.class;

? ? ? ? }

? ? ? ? Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

? ? ? ? if (index >= params.length || index < 0) {

? ? ? ? ? ? return Object.class;

? ? ? ? }

? ? ? ? if (!(params[index] instanceof Class)) {

? ? ? ? ? ? return Object.class;

? ? ? ? }

? ? ? ? return (Class) params[index];

? ? }

}?

轉載來源:? http://blog.csdn.net/wangjunjun2008/article/details/43970217? http://www.hankcs.com/program/t-class.html? https://www.cnblogs.com/sirab415/p/6133533.html

總結

以上是生活随笔為你收集整理的[转载] Java获取泛型T的类型 T.class的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。