一、實例目標
根據傳入的完整類名字符串類名,實現創建對應類的實例
根據傳入的類實例,以及傳入的方法名字符串,實現動態調用指定的方法,返回方法的返回值
在FanSheTest 單元測試中實現使用FanShe類傳入"cn.com.rwq.test.Entity"字符串實現創建Entity類,並且根據傳入的字符串動態調用類中的與字符串同名的方法
二、代碼
1、測試類
- package cn.com.rwq.test;
-
- import junit.framework.TestCase;
-
- public class FanSheTest extends TestCase {
- private FanShe fanShe ;
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- fanShe = new FanShe() ;
- }
-
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
-
- }
-
- public void testCreateClass() throws Exception{
- Object object = fanShe.createClass("cn.com.rwq.test.Entity");
- assertNotNull(object);
- Common common = (Common)object;
- assertEquals("123456789", common.getName());
- assertEquals("12345678", ((Entity)object).toString());
-
- }
-
- public void testCreateObject() throws Exception{
- Object object = fanShe.createClass("cn.com.rwq.test.Entity");
- fanShe.createObject(object, "print");
-
- // fanShe.createObject(object, "two");
- // strPrint
- // fanShe.createObject(object, "siyou");
-
- String a =(String)fanShe.createObject(object, "strPrint");
- assertEquals("abs", a);
-
- int b =(int)fanShe.createObject(object, "intPrint");
- assertEquals(123, b);
- Common common = (Common)object;
- fanShe.createObject(common, "printName");
- }
- }
2、反射了的實現
- package cn.com.rwq.test;
-
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
-
- public class FanShe {
- /**
- * 根據完整類名創建對應類的實體
- * @param className 完整類名
- * @return 創建的實體
- */
- public Object createClass(String className)
- throws ClassNotFoundException, InstantiationException, IllegalAccessException {
- Class clazz = Class.forName(className);
- Method m[] = clazz.getDeclaredMethods();
- for(Method one : m){
- System.out.println(one.toString());
- }
- Object object= clazz.newInstance();
- return object;
- }
- /**
- * 根據類,以及方法名,動態調用方法
- * @param object 類
- * @param actionName 方法名
- * @return 調用的方法的返回值
- */
- public static Object createObject(Object object,String actionName)
- throws ClassNotFoundException, InstantiationException, IllegalAccessException {
- Method aMethod;
- try {
- aMethod = object.getClass().getMethod(actionName,null);
- return aMethod.invoke(object,null);
- } catch (NoSuchMethodException | SecurityException e1) {
- e1.printStackTrace();
- } catch (IllegalArgumentException | InvocationTargetException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
3、javaBean
- package cn.com.rwq.test;
- /**
- * javaBean
- * @author Administrator
- */
- public class Entity extends Common {
-
- public Entity(){
- System.out.println("Entity 構造方法");
- }
- public void print(){
- System.out.println("執行printe 無返回值");
- }
- void two(){
- System.out.println("執行two 方法");
- }
- private void siyou(){
- System.out.println("執行siyou 私有方法");
- }
- public String strPrint(){
- System.out.println("執行strPrint 有返回值");
- return "abs";
- }
- public int intPrint(){
- System.out.println("執行intPrint 有返回值");
- return 123;
- }
- public void printName(){
- System.out.println("11111111 "+super.getName());
- }
- public String toString(){
- return "12345678";
- }
- public static void main(String[] args){
- Entity fanshe = new Entity();
- fanshe.print();
- fanshe.two();
- fanshe.siyou();
- System.out.println(fanshe.strPrint());
- }
- }
4、父類
- package cn.com.rwq.test;
- /**
- * 父類
- */
- public class Common {
- private String name = "123456789";
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
- }
三、重點理解
1.待實現的實體類必須有無參的構造函數
2. Class<?> demo= Class.forName(""); 根據完整類名的字符串得到指定的類
3.取得一個類的全部框架
- Class<?> demo = Class.forName("cn.com.rwq.test.Entity");
- System.out.println("===============本類屬性========================");
- // 取得本類的全部屬性
- Field[] field = demo.getDeclaredFields();
- for (int i = 0; i < field.length; i++) {
- // 權限修飾符
- int mo = field[i].getModifiers();
- String priv = Modifier.toString(mo);
- // 屬性類型
- Class<?> type = field[i].getType();
- System.out.println(priv + " " + type.getName() + " "
- + field[i].getName() + ";");
- }
- System.out.println("=========實現的接口或者父類的屬性==============");
- // 取得實現的接口或者父類的屬性
- Field[] filed1 = demo.getFields();
- for (int j = 0; j < filed1.length; j++) {
- // 權限修飾符
- int mo = filed1[j].getModifiers();
- String priv = Modifier.toString(mo);
- // 屬性類型
- Class<?> type = filed1[j].getType();
- System.out.println(priv + " " + type.getName() + " "
- + filed1[j].getName() + ";");
- }
- }
- }
4.//調用無參方法(Class<?> demo 實例化的類)
Method method=demo.getMethod("方法名");
method.invoke(demo.newInstance());
//調用有參數的方法
method=demo.getMethod("方法名", String.class,int.class);
method.invoke(demo.newInstance(),"Rollen",20);