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


Java Method類 getParameterCount()用法及代碼示例


Method類的java.lang.reflect.Method.getParameterCount()方法返回在方法Object上聲明的參數數量。

用法:

public int getParameterCount()

返回值:此方法返回在此方法對象上定義的形式參數的數量。



以下示例程序旨在說明Method類的getParameterCount()方法:

範例1:在下麵的程序中,對於作為輸入給出的特定方法,返回參數編號。

// Program Demonstrate how to apply getParameterCount() 
// 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 list of methods 
            Method[] methods = classobj.getMethods(); 
  
            // get no of parameters for method setManyValues 
            int noOfParameters = methods[0].getParameterCount(); 
  
            System.out.println("Method Name:"
                               + methods[0].getName()); 
  
            // print no of parameters 
            System.out.println("No of parameters:" + noOfParameters); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
  
    // method name setManyValues 
    // No of parameters is one for this method 
    public void setManyValues(String parameter1) 
    { 
        System.out.println("setManyValues"); 
    } 
}
輸出:
Method Name:setManyValues
No of parameters:1

範例2:在程序下麵,返回在類中定義的所有方法的參數編號。

// Program Demonstrate how to apply getParameterCount() 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 = DemoClass.class; 
  
            // get list of methods 
            Method[] methods = classobj.getMethods(); 
  
            // get no of parameters for each 
            // method in list of methods 
            for (Method method:methods) { 
  
                // print name of method 
                System.out.print("Method Name is "
                                 + method.getName()); 
  
                // get no of parameters for each method 
                int noOfParameters = method.getParameterCount(); 
  
                // print no of parameters 
                System.out.println(" and No of parameters = "
                                   + noOfParameters); 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
} 
// Demo class to apply getParameterCount() method 
class DemoClass { 
  
    // method name DemoMethod1 
    // No of parameters is 1 for this method 
    public void DemoMethod1(String parameter1) 
    { 
        System.out.println("DemoMethod1"); 
    } 
  
    // method name DemoMethod2 
    // No of parameters is 2 for this method 
    public void DemoMethod2(String parameter1, 
                            String parameter2) 
    { 
        System.out.println("DemoMethod2"); 
    } 
  
    // method name DemoMethod2 
    // No of parameters is Zero for this method 
    public void DemoMethod3() 
    { 
        System.out.println("DemoMethod3"); 
    } 
}
輸出:
Method Name is DemoMethod1 and No of parameters = 1
Method Name is DemoMethod2 and No of parameters = 2
Method Name is DemoMethod3 and No of parameters = 0
Method Name is wait and No of parameters = 2
Method Name is wait and No of parameters = 1
Method Name is wait and No of parameters = 0
Method Name is equals and No of parameters = 1
Method Name is toString and No of parameters = 0
Method Name is hashCode and No of parameters = 0
Method Name is getClass and No of parameters = 0
Method Name is notify and No of parameters = 0
Method Name is notifyAll and No of parameters = 0

說明:該程序的輸出還顯示除類對象中定義的方法(例如,wait,equals,toString,hashCode,getClass,notify,notifyAll)以外的方法對象的結果。這些方法通過類對象從java.lang lang包的超類名稱Object繼承。

參考: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getParameterCount-




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | getParameterCount() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。