java.lang.reflect.Method.getReturnType() 方法示例

描述

java.lang.reflect.Method.getReturnType() 方法返回一个 Class 对象,该对象表示此 Method 对象所表示的方法的正式返回类型。

声明

以下是 java.lang.reflect.Method.getReturnType() 方法的声明。

public Class<?> getReturnType()

返回

此对象所表示的方法的返回类型。

示例

以下示例显示了 java.lang.reflect.Method.getReturnType() 方法的用法。

package com.tutorialspoint;

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;
   } 
}

让我们编译并运行上述程序,这将产生以下结果 −

java.lang.String

java_reflect_method.html



关于我们 | 隐私条款 | 学习路线
返回顶部