java反射:載入成員方法

來源:互聯網
上載者:User

標籤: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反射:載入成員方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.