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-
相关用法
- Java Constructor getParameterCount()用法及代码示例
- Java Method类 getDeclaredAnnotations()用法及代码示例
- Java Method类 getAnnotation()用法及代码示例
- Java Method类 isVarArgs()用法及代码示例
- Java Method类 getGenericParameterTypes()用法及代码示例
- Java Method类 isBridge()用法及代码示例
- Java Method类 getName()用法及代码示例
- Java Method类 toGenericString()用法及代码示例
- Java Method类 getParameterAnnotations()用法及代码示例
- Java Method类 getGenericExceptionTypes()用法及代码示例
- Java Method类 getParameterTypes()用法及代码示例
- Java Method类 getAnnotatedReturnType()用法及代码示例
- Java Method类 getReturnType()用法及代码示例
- Java Method类 hashCode()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Method Class | getParameterCount() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。