java反射:加载成员方法
package com.ma.reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
import com.ma.bean.StudentBean;
/**
* 反射:加载成员方法
* @author ma
*
*/
public class Demo3 {
/**
* 测试无参成员 方法
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
@Test
public void test1() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
//加载类
Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
//加载方法
Method method = clazz.getMethod("eat",null);
//调用方法
method.invoke(new StudentBean(), null);
}
/**
* 测试有参成员 方法
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
@Test
public void test2() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
//加载类
Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
//加载方法
Method method = clazz.getMethod("play", String.class,int.class);
//调用方法
method.invoke(new StudentBean(), "张三",45);
}
/**
* 测试可变参成员 方法
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
@Test
public void test3() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
//加载类
Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
//加载方法
Method method = clazz.getMethod("run", int[].class);
//调用方法
method.invoke(new StudentBean(), new int[]{1,2,3});
}
/**
* 测试私有成员 方法
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
@Test
public void test4() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
//加载类
Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
//加载方法
Method method = clazz.getDeclaredMethod("jump", null);
//设置私有方法访问权限
method.setAccessible(true);
//调用方法
method.invoke(new StudentBean(), null);
}
/**
* 测试静态成员方法
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
@Test
public void test5() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
//加载类
Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
//加载方法
Method method = clazz.getMethod("sleep", int.class);
//调用方法,静态方法不要对象就可以调用方法
method.invoke(null, 34);
}
/**
* main方法的测试
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
@Test
public void test6() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
//加载类
Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
//加载方法
Method method = clazz.getMethod("main", String[].class);
//调用main方法,静态方法不要对象就可以调用方法,由于main方法要折封数组,所以有两种方法调用main方法
//第一种调用
//method.invoke(null, new Object[]{new String[]{"aa","bb"}});
//第二种调用
method.invoke(null, (Object)new String[]{"aa","cc"});
}
}
StudentBean类
package com.ma.bean;
/**
* 学生类
* @author ma
*
*/
public class StudentBean {
public String name = "刘备";
public int age;
private String work = "教师";
private static String sex = "男";
public StudentBean() {
super();
}
public void eat(){
System.out.println("吃饭了。。。。。。。。。。。。。。");
}
public void play(String name,int age){
System.out.println(name+":去玩了哟。。。。。。。。。"+age);
}
public void run(int ...intaa){
System.out.println(":这是可变参数方法");
}
private void jump(){
System.out.println("这是私有方法");
}
public static void sleep(int age){
System.out.println(age+":这是静态方法");
}
public static void main(String[] args) {
System.out.println("这是main方法!!!");
}
}