先决条件 : JAVAJava语言中的lang.Class |集1 , JAVAJava语言中的lang.Class |集合2 这个 JAVA朗,反思 方法类 帮助获取类或接口上单个方法的信息。该类还提供对类的方法的访问,并在运行时调用它们。 方法类的getReturnType()方法 每个方法都有一个返回类型,无论它是void、int、double、string还是任何其他数据类型。这个 getReturnType() 方法类的方法返回一个类对象,该类对象表示在创建方法时在方法中声明的返回类型。 语法:
null
public Class<?> getReturnType()
参数: 该方法不采用任何参数。 返回值: 该方法返回一个类对象,该类对象表示该方法对象的形式返回类型。 下面的程序演示了method类的getReturnType()方法: 项目1: 下面的程序打印一个类的某些特定方法的返回类型,该类在程序的主方法中作为输入提供。
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( "Method 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 : setManyValuesReturn Type Details: voidMethod Name : getValueReturn Type Details: java.lang.StringMethod Name : setValueReturn 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( "Method 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 : method3Return Type Details: voidMethod Name : method2Return Type Details: java.lang.StringMethod Name : method1Return Type Details: intMethod Name : mainReturn Type Details: voidMethod Name : waitReturn Type Details: voidMethod Name : waitReturn Type Details: voidMethod Name : waitReturn Type Details: voidMethod Name : equalsReturn Type Details: booleanMethod Name : toStringReturn Type Details: java.lang.StringMethod Name : hashCodeReturn Type Details: intMethod Name : getClassReturn Type Details: java.lang.ClassMethod Name : notifyReturn Type Details: voidMethod Name : notifyAllReturn Type Details: void
说明: 这个程序的输出还显示了除类对象中定义的方法之外的方法对象的结果,比如wait、equals、toString、hashCode、getClass、notify、notifyAll。这些方法继承自java的超级类名对象。lang lang按类对象打包。 参考: 用于getReturnType()的Oracle文档
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END