java.lang.reflect.Field.getGenericType() 方法示例
描述
java.lang.reflect.Field.getGenericType() 方法返回一个 Type 对象,该对象表示此 Field 对象所表示的字段的声明类型。
声明
以下是 java.lang.reflect.Field.getGenericType() 方法的声明。
public Type getGenericType()
返回
一个 Type 对象,该对象表示此 Field 对象所表示的字段的声明类型。
异常
GenericSignatureFormatError −如果通用字段签名不符合 Java 虚拟机规范中指定的格式。
TypeNotPresentException − 如果指定的对象不是声明底层字段(或其子类或实现者)的类或接口的实例。
MalformedParameterizedTypeException − 如果底层字段的通用签名引用了由于任何原因无法实例化的参数化类型。
示例
以下示例显示了 java.lang.reflect.Field.getGenericType() 方法的用法。
package com.tutorialspoint; import java.lang.reflect.Field; public class FieldDemo { public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { SampleClass sampleObject = new SampleClass(); Field field = SampleClass.class.getField("sampleField"); System.out.println(field.getGenericType()); } } class SampleClass { public static float sampleField = 5.0f; }
让我们编译并运行上述程序,这将产生以下结果 −
float