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


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


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

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

方法类的getParameterTypes()方法:
为了创建方法,需要很多参数才能使这些方法正常工作。 Method类的getParameterTypes()方法返回一个Class对象数组,这些对象表示参数类型,这些类型在编码时在method中声明。如果方法对象不带参数,则getParameterTypes()返回长度为0的数组。



用法:

public Class[] getParameterTypes()

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

返回值:该方法返回一个Class对象数组,该数组按声明顺序表示该方法对象的形式参数类型。

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

程序1:下面的程序将打印Class对象数组的详细信息,这些对象表示程序中定义的方法对象的形式参数类型。

/* 
* Program Demonstrate how to apply getParameterTypes() 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) { 
  
                // 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 getGenericParameterTypes() method 
                    Class[] parameters = method.getParameterTypes(); 
  
                    // Print parameter Types of method Object 
                    System.out.println("\nMethod Name:"
                                       + method.getName()); 
                    System.out.println("No of Parameters:"
                                       + parameters.length); 
                    System.out.println("Parameter object details:"); 
                    for (Class classobject:parameters) { 
                        System.out.println(classobject.getName()); 
                    } 
                } 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
    // Method containing two parameter 
    public void setValue(String value1, String value2) 
    { 
        System.out.println("setValue"); 
    } 
  
    // Method containing no parameter 
    public String getValue() 
    { 
        System.out.println("getValue"); 
        return "getValue"; 
    } 
  
    // Method containing many parameter 
    public void setManyValues(int value1, double value2, String value3) 
    { 
        System.out.println("setManyValues"); 
    } 
}
输出:
Method Name:setManyValues
No of Parameters:3
Parameter object details:
int
double
java.lang.String

Method Name:getValue
No of Parameters:0
Parameter object details:

Method Name:setValue
No of Parameters:2
Parameter object details:
java.lang.String
java.lang.String

程序2:我们提供一个参数类对象作为输入,如果Method对象包含参数,则程序下面将计算这些类型参数的数量,否则程序返回0。

/* 
* Program Demonstrate how to apply getParameterTypes() method 
* of Method Class. 
*/
import java.lang.reflect.Method; 
import java.lang.reflect.Type; 
  
public class GFG3 { 
  
    // Main method 
    public static void main(String[] args) 
    { 
        try { 
            // Create class object 
            Class classobj = sample.class; 
  
            Method[] methods = classobj.getMethods(); 
  
            /* Check whether setManyValues() method contains  
                        int parameter or not 
                and print no of string parameter it contains*/
  
            for (Method method:methods) { 
                if (method.getName().equals("setValue")) { 
                    int count = containsParameter(method,  
                               (Class)java.lang.String.class); 
                    System.out.println("No of String "+ 
                        "Parameters in setValue():" + count); 
                } 
            } 
  
            /* Check whether setManyValues() method contains  
                        int parameter or not 
            and print no of string parameter it contains */
  
            for (Method method:methods) { 
                if (method.getName().equals("setManyValues")) { 
                    int count = containsParameter(method,  
                                            (Class) int.class); 
                    System.out.println("No of int Parameters"+ 
                             " in setManyValues():" + count); 
                } 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
  
    // Count no of parameters contain by method same as passed to method 
    private static int containsParameter(Method method, Class parameterName) 
    { 
        int count = 0; 
  
        // Get all parameter class objects  using getParameterTypes() 
        Class parameters[] = method.getParameterTypes(); 
  
        for (int i = 0; i < parameters.length; i++) { 
            // Check contains parameter or not 
            if (parameters[i] == parameterName) { 
                count++; 
            } 
        } 
  
        return count; 
    } 
} 
// A simple class 
class sample { 
  
    // Method containing two parameter 
    public void setValue(String value1, String value2) 
    { 
        System.out.println("setValue"); 
    } 
  
    // Method containing many parameter 
    public void setManyValues(int value1, double value2, String value3) 
    { 
        System.out.println("setManyValues"); 
    } 
}
输出:
No of String Parameters in setValue():2
No of int Parameters in setManyValues():1

参考:
Oracle Doc for getParameterTypes()




相关用法


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