Java Field.getGenericType()/getType()方法的功能及区别说明
下文将讲述Field.getGenericType()/getType()方法的功能及不同点说明,如下所示:
Field.getGenericType()/getType()方法的功能:
getType():返回属性声明时类型对象(返回class对象)
getGenericType():返回属性声的Type类型
------------------------------------------------------------
getType() 和 getGenericType()的不同之处:
1.两者返回类型不同,一个是Class对象一个是Type接口
2.当属性是一个泛型,从getType()只能得到这个属性的接口类型
从getGenericType()还能得到这个泛型的参数类型。
3.getGenericType()
当前属性有签名属性类型就返回,否则就返回Field.getType()
例:
package com.java265.other;
import java.lang.reflect.Field;
public class TestClass {
public static void main(String[] args) throws Exception {
Class clazz = Class.forName("com.java265.other.User");
Field f = clazz.getDeclaredField("Name");
System.out.println(f.getType());
System.out.println(f.getGenericType());
}
}
class User<T> {
private int age;
private T Name;
public User() {
}
}
------运行以上代码,将输出以下信息----
class java.lang.Object
T
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


