歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java反射可執行的實例

Java反射可執行的實例

日期:2017/3/1 10:06:19   编辑:Linux編程

一、實例目標

根據傳入的完整類名字符串類名,實現創建對應類的實例

根據傳入的類實例,以及傳入的方法名字符串,實現動態調用指定的方法,返回方法的返回值

在FanSheTest 單元測試中實現使用FanShe類傳入"cn.com.rwq.test.Entity"字符串實現創建Entity類,並且根據傳入的字符串動態調用類中的與字符串同名的方法

二、代碼

1、測試類

  1. package cn.com.rwq.test;
  2. import junit.framework.TestCase;
  3. public class FanSheTest extends TestCase {
  4. private FanShe fanShe ;
  5. @Override
  6. protected void setUp() throws Exception {
  7. super.setUp();
  8. fanShe = new FanShe() ;
  9. }
  10. @Override
  11. protected void tearDown() throws Exception {
  12. super.tearDown();
  13. }
  14. public void testCreateClass() throws Exception{
  15. Object object = fanShe.createClass("cn.com.rwq.test.Entity");
  16. assertNotNull(object);
  17. Common common = (Common)object;
  18. assertEquals("123456789", common.getName());
  19. assertEquals("12345678", ((Entity)object).toString());
  20. }
  21. public void testCreateObject() throws Exception{
  22. Object object = fanShe.createClass("cn.com.rwq.test.Entity");
  23. fanShe.createObject(object, "print");
  24. // fanShe.createObject(object, "two");
  25. // strPrint
  26. // fanShe.createObject(object, "siyou");
  27. String a =(String)fanShe.createObject(object, "strPrint");
  28. assertEquals("abs", a);
  29. int b =(int)fanShe.createObject(object, "intPrint");
  30. assertEquals(123, b);
  31. Common common = (Common)object;
  32. fanShe.createObject(common, "printName");
  33. }
  34. }

2、反射了的實現

  1. package cn.com.rwq.test;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. public class FanShe {
  5. /**
  6. * 根據完整類名創建對應類的實體
  7. * @param className 完整類名
  8. * @return 創建的實體
  9. */
  10. public Object createClass(String className)
  11. throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  12. Class clazz = Class.forName(className);
  13. Method m[] = clazz.getDeclaredMethods();
  14. for(Method one : m){
  15. System.out.println(one.toString());
  16. }
  17. Object object= clazz.newInstance();
  18. return object;
  19. }
  20. /**
  21. * 根據類,以及方法名,動態調用方法
  22. * @param object 類
  23. * @param actionName 方法名
  24. * @return 調用的方法的返回值
  25. */
  26. public static Object createObject(Object object,String actionName)
  27. throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  28. Method aMethod;
  29. try {
  30. aMethod = object.getClass().getMethod(actionName,null);
  31. return aMethod.invoke(object,null);
  32. } catch (NoSuchMethodException | SecurityException e1) {
  33. e1.printStackTrace();
  34. } catch (IllegalArgumentException | InvocationTargetException e) {
  35. e.printStackTrace();
  36. }
  37. return null;
  38. }
  39. }

3、javaBean

  1. package cn.com.rwq.test;
  2. /**
  3. * javaBean
  4. * @author Administrator
  5. */
  6. public class Entity extends Common {
  7. public Entity(){
  8. System.out.println("Entity 構造方法");
  9. }
  10. public void print(){
  11. System.out.println("執行printe 無返回值");
  12. }
  13. void two(){
  14. System.out.println("執行two 方法");
  15. }
  16. private void siyou(){
  17. System.out.println("執行siyou 私有方法");
  18. }
  19. public String strPrint(){
  20. System.out.println("執行strPrint 有返回值");
  21. return "abs";
  22. }
  23. public int intPrint(){
  24. System.out.println("執行intPrint 有返回值");
  25. return 123;
  26. }
  27. public void printName(){
  28. System.out.println("11111111 "+super.getName());
  29. }
  30. public String toString(){
  31. return "12345678";
  32. }
  33. public static void main(String[] args){
  34. Entity fanshe = new Entity();
  35. fanshe.print();
  36. fanshe.two();
  37. fanshe.siyou();
  38. System.out.println(fanshe.strPrint());
  39. }
  40. }

4、父類

  1. package cn.com.rwq.test;
  2. /**
  3. * 父類
  4. */
  5. public class Common {
  6. private String name = "123456789";
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. }

三、重點理解
1.待實現的實體類必須有無參的構造函數
2. Class<?> demo= Class.forName(""); 根據完整類名的字符串得到指定的類
3.取得一個類的全部框架

  1. Class<?> demo = Class.forName("cn.com.rwq.test.Entity");
  2. System.out.println("===============本類屬性========================");
  3. // 取得本類的全部屬性
  4. Field[] field = demo.getDeclaredFields();
  5. for (int i = 0; i < field.length; i++) {
  6. // 權限修飾符
  7. int mo = field[i].getModifiers();
  8. String priv = Modifier.toString(mo);
  9. // 屬性類型
  10. Class<?> type = field[i].getType();
  11. System.out.println(priv + " " + type.getName() + " "
  12. + field[i].getName() + ";");
  13. }
  14. System.out.println("=========實現的接口或者父類的屬性==============");
  15. // 取得實現的接口或者父類的屬性
  16. Field[] filed1 = demo.getFields();
  17. for (int j = 0; j < filed1.length; j++) {
  18. // 權限修飾符
  19. int mo = filed1[j].getModifiers();
  20. String priv = Modifier.toString(mo);
  21. // 屬性類型
  22. Class<?> type = filed1[j].getType();
  23. System.out.println(priv + " " + type.getName() + " "
  24. + filed1[j].getName() + ";");
  25. }
  26. }
  27. }

4.//調用無參方法(Class<?> demo 實例化的類)
Method method=demo.getMethod("方法名");
method.invoke(demo.newInstance());
//調用有參數的方法
method=demo.getMethod("方法名", String.class,int.class);
method.invoke(demo.newInstance(),"Rollen",20);

Copyright © Linux教程網 All Rights Reserved