java.lang.reflect.Method.getGenericReturnType()方法返回一个Type对象,该对象表示此Method对象表示的方法的正式返回类型。
声明
以下是java.lang.reflect.Method.getGenericReturnType()方法的声明。
public Type getGenericReturnType()
参数
NA
返回值
一个Type对象,表示底层方法的正式返回类型。
异常
GenericSignatureFormatError - 如果通用方法签名不符合Java虚拟机规范中指定的格式。
TypeNotPresentException - 如果底层方法的throws子句引用不存在的类型声明。
MalformedParameterizedTypeException - 如果底层方法的throws子句引用了由于任何原因无法实例化的参数化类型。
以下示例显示java.lang.reflect.Method.getGenericReturnType()方法的用法。
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class MethodDemo {
public static void main(String[] args) {
Method[] methods = SampleClass.class.getMethods();
Type returnType = methods[0].getGenericReturnType();
System.out.println(returnType);
}
}
class SampleClass {
private String sampleField;
public String getSampleField() {
return sampleField;
}
public void setSampleField(String sampleField) {
this.sampleField = sampleField;
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
class java.lang.String
¥ 我要打赏
纠错/补充
收藏
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。