Method类提供了对类成员方法的操作方法,应用这些方法可以获取成员方法的名称、参数个数、参数类型,也可以执行方法。Method类在java.lang.reflect包内。
Method类定义的主要方法说明如下。
● String getName()
该方法用于获取成员方法的名称。
● Class<?>[] getParameterTypes()
该方法返回一个类对象数组,该数组按声明顺序存储该方法的形式参数类型。
● int getParameterCount()
该方法返回方法的参数个数。
● Class<?> getReturnType()
该方法返回一个类对象,该类对象表示此方法的返回类型。
● Object invoke(Object obj, Object... args)
该方法用于执行obj对象上的方法,方法的参数由args指定。
● Class<?>[] getExceptionTypes()
该方法返回一个类对象数组,该数组表示由该方法可能引发的异常类型。
● int getModifiers()
该方法返回一个整数,该整数可用于Modifier获取修饰符方法的传入参数。
案例1:建立MethodTest2测试类,应用Class类的getMethods()方法获取Person类的公共成员方法,输出每个成员方法的名称、返回类型、参数个数和参数类型。
在method包下新建MethodTest2测试类。代码如下:
package method;
import java.lang.reflect.Method;
import constructor.Person;
/**
* @ClassName: MethodTest2
* @Description: 反射(成员方法的相关操作)案例1
* @author 编程训练营
* @date 2020年1月3日
*
*/
public class MethodTest2 {
/**
* @Title: main
* @Description: Java程序入口main方法
* @param @param args 参数
* @return void 返回类型
* @throws
*/
public static void main(String[] args) {
// 获取Person类的Class对象
Class<?> personClass = (Class) Person.class;
// 通过class.getDeclaredMethods获取所有的成员方法
Method[] methods = personClass.getDeclaredMethods();
for (Method method : methods) {
System.out.println("-------成员方法-------");
// 输出方法名称
System.out.println("方法名称:" + method.getName());
// 输出方法的返回类型
System.out.println("方法返回类型:" + method.getReturnType());
// 输出方法的参数个数
System.out.println("方法参数个数:" + method.getParameterCount());
// 输出方法的参数类型
if( method.getParameterCount() > 0 )
{
//通过method.getParameterTypes()获取参数类数组(Class)
Class[] paramClasses = method.getParameterTypes();
int nParamCount = 1;
for (Class paramClass : paramClasses) {
System.out.printf("参数%d:%s\n",nParamCount,paramClass.getName());
nParamCount++;
}
}
}
}
}MethodTest2程序首先获取Person类的Class对象,然后调用Class对象的getDeclaredMethods()获取Person类的所有成员方法。遍历getDeclaredMethods()方法返回的Method类型的成员方法数组,在遍历过程中,输出每个方法的名称、返回类型、参数个数和参数类型。
程序执行结果如下图所示:
案例2:建立MethodTest3测试类,获取Person类的构造方法并实例化person对象。获取Person对象成员变量的set方法,并执行set方法。获取person对象的showPerson方法,并执行该方法。
在method包下新建MethodTest3测试类。代码如下:
package method;
import java.lang.reflect.Method;
import constructor.Person;
/**
* @ClassName: MethodTest3
* @Description: 反射(成员方法的相关操作)案例2
* @author 编程训练营
* @date
*
*/
public class MethodTest3 {
/**
* @Title: main
* @Description: MethodTest2
* @param @param args 参数
* @return void 返回类型
* @throws
*/
public static void main(String[] args) {
try {
// 获取Person类的Class对象
Class<?> classPerson = Class.forName("constructor.Person");
//设置Person类的setName方法名
String setName = "setName";
//设置Person类的setAge方法名
String setAge = "setAge";
//设置Person类的setJob方法名
String setJob = "setJob";
//设置Person类的setIncome方法名
String setIncome = "setIncome";
//设置Person类的showPerson方法名
String showPerson = "showPerson";
// 实例化Person对象
Person person = (Person)classPerson.newInstance();
//使用Class对象的getMethod方法获取Person类的公开方法
Method methodName = classPerson.getMethod(setName,String.class);
// 使用Method的invoke方法执行methodName
methodName.invoke(person,"张三");
//使用Class对象的getMethod方法获取Person类的公开方法
Method methodAge = classPerson.getMethod(setAge,int.class);
// 使用Method的invoke方法执行methodAge
methodAge.invoke(person,26);
//使用Class对象的getMethod方法获取Person类的公开方法
Method methodJob = classPerson.getMethod(setJob,String.class);
// 使用Method的invoke方法执行methodJob
methodJob.invoke(person,"工程师");
//使用Class对象的getMethod方法获取Person类的公开方法
Method methodIncome = classPerson.getMethod(setIncome,double.class);
// 使用Method的invoke方法执行methodIncome
methodIncome.invoke(person,150000);
//使用Class对象的getMethod方法获取Person类的公开方法
Method methodShowPerson = classPerson.getMethod(showPerson);
methodShowPerson.invoke(person);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}MethodTest3程序应用Class类的静态方法forName()获取Person类的Class对象,并实例化person对象。然后应用Class对象的getMethod()方法获取person对象的成员变量的set()方法,并应用Method类的invoke()方法执行set()方法。最后执行person对象的showPerson()方法。
程序执行结果如下图所示:
