標籤:lang 可變參 import highlight com private sys dem static
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 */@Testpublic 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 */@Testpublic 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 */@Testpublic 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 */@Testpublic 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 */@Testpublic 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 */@Testpublic 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方法!!!");}}
java反射:載入成員方法