java.lang.reflect.Method.getReturnType()
方法返回一個Class
物件,該物件表示此Method
物件表示的方法的正式返回型別。
以下是java.lang.reflect.Method.getReturnType()
方法的宣告。
public Class<?> getReturnType()
引數
返回值
異常
以下範例顯示java.lang.reflect.Method.getReturnType()
方法的用法。
package com.yiibai;
import java.lang.reflect.Method;
public class MethodDemo {
public static void main(String[] args) {
Method[] methods = SampleClass.class.getMethods();
Class returnType = methods[0].getReturnType();
System.out.println(returnType.getName());
}
}
class SampleClass {
private String sampleField;
public String getSampleField() {
return sampleField;
}
public void setSampleField(String sampleField) {
this.sampleField = sampleField;
}
}
讓我們編譯並執行上面的程式,這將產生以下結果 -
void