當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。