当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Method类 getReturnType()用法及代码示例


先决条件:Java中的Java.lang.Class类|设置1,Java中的Java.lang.Class类。套装2

java.lang.reflectMethod类有助于获取类或接口上单个方法的信息。此类还提供对类方法的访问,并在运行时调用它们。

方法类的getReturnType()方法
每个方法都有一个返回类型,无论它是void,int,double,string还是任何其他数据类型。 Method类的getReturnType()方法返回一个代表返回类型的Class对象,该对象在创建方法时在method中声明。



用法:

public Class<?> getReturnType()

参数:该方法不带任何参数。

返回值:该方法返回一个Class对象,该对象表示方法对象的形式返回类型。

以下示例程序旨在说明Method类的getReturnType()方法:

程序1:程序下方打印了作为程序主要方法输入提供的类的某些特定方法的返回类型。

/* 
* Program Demonstrate how to apply getReturnType() method 
* of Method Class. 
*/
import java.lang.reflect.Method; 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
        try { 
            // Create class object 
            Class classobj = demoForReturnParam.class; 
  
            // Get Method Object 
            Method[] methods = classobj.getMethods(); 
  
            // Iterate through methods 
            for (Method method:methods) { 
  
                // We are only taking method defined in the demo class 
                // We are not taking other methods of the object class 
                if (method.getName().equals("setValue") 
                    || method.getName().equals("getValue") 
                    || method.getName().equals("setManyValues")) { 
                    // apply getReturnType() method 
                    Class returnParam = method.getReturnType(); 
  
                    // print return Tyoe class object of method Object 
                    System.out.println("\nMethod Name:"
                                       + method.getName()); 
  
                    System.out.println("Return Type Details:" + returnParam.getName()); 
                } 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
} 
  
// A simple class 
class demoForReturnParam { 
  
    // Method returning int value 
    public int setValue() 
    { 
        System.out.println("setValue"); 
        return 24; 
    } 
  
    // Method returning string value 
    public String getValue() 
    { 
        System.out.println("getValue"); 
        return "getValue"; 
    } 
  
    // Method returning nothing 
    public void setManyValues(int value1, String value3) 
    { 
        System.out.println("setManyValues"); 
    } 
}
输出:
Method Name:setManyValues
Return Type Details:void

Method Name:getValue
Return Type Details:java.lang.String

Method Name:setValue
Return Type Details:int

程序2:程序下面为程序的主方法中提供的类的所有方法打印返回类型。

/* 
* Program Demonstrate how to apply getReturnType() method 
* of Method Class. 
*/
import java.lang.reflect.Method; 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
        try { 
            // Create class object 
            Class classobj = GFG.class; 
  
            // Get Method Object 
            Method[] methods = classobj.getMethods(); 
  
            // Iterate through methods 
            for (Method method:methods) { 
  
                // Apply getReturnType() method 
                Class returnParam = method.getReturnType(); 
  
                // Print return Type class object of method Object 
                System.out.println("\nMethod Name:"
                                   + method.getName()); 
  
                System.out.println("Return Type Details:" + returnParam.getName()); 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    // Method returning int value 
    public int method1() 
    { 
        System.out.println("method1"); 
        return 24; 
    } 
  
    // Method returning string value 
    public String method2() 
    { 
        System.out.println("method2"); 
        return "method3"; 
    } 
  
    // Method returning nothing 
    public void method3(int value1, String value3) 
    { 
        System.out.println("method3"); 
    } 
}
输出:
Method Name:method3
Return Type Details:void

Method Name:method2
Return Type Details:java.lang.String

Method Name:method1
Return Type Details:int

Method Name:main
Return Type Details:void

Method Name:wait
Return Type Details:void

Method Name:wait
Return Type Details:void

Method Name:wait
Return Type Details:void

Method Name:equals
Return Type Details:boolean

Method Name:toString
Return Type Details:java.lang.String

Method Name:hashCode
Return Type Details:int

Method Name:getClass
Return Type Details:java.lang.Class

Method Name:notify
Return Type Details:void

Method Name:notifyAll
Return Type Details:void

说明:该程序的输出还显示除类对象中定义的方法(例如,wait,equals,toString,hashCode,getClass,notify,notifyAll)以外的方法对象的结果。这些方法是通过class对象继承自java.lang lang包的超类名称Object的。

参考:
Oracle Doc for getReturnType()




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Method Class | getReturnType() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。