📜  方法类 | Java中的 getReturnType() 方法

📅  最后修改于: 2022-05-13 01:54:25.862000             🧑  作者: Mango

方法类 | Java中的 getReturnType() 方法

先决条件: Java.lang.Class 类在Java |设置1、 Java.lang.Class 类Java |设置 2
Java.lang.reflect方法类有助于获取类或接口上单个方法信息。此类还提供对类方法的访问并在运行时调用它们。
Method类的getReturnType()方法
每个方法都有一个返回类型,无论它是 void、int、double、 字符串还是任何其他数据类型。 Method 类的getReturnType()方法返回一个 Class 对象,表示返回类型,在创建方法时在方法中声明。
句法:

public Class getReturnType()

参数:该方法不带任何参数。
返回值:方法返回一个Class对象,表示方法对象的正式返回类型。
下面的程序说明了 Method 类的 getReturnType() 方法:
程序 1:下面的程序打印类的某些特定方法的返回类型,这些方法在程序的 main 方法中作为输入提供。

Java
/*
* 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 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();
        }
    }
}
 
// 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");
    }
}


Java
/*
* 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 : setManyValues
Return Type Details: void

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

Method Name : setValue
Return Type Details: int

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

Java

/*
* 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。这些方法由类对象继承自Java.lang lang 包的超类名 Object。
参考:
getReturnType() 的 Oracle 文档