zoukankan      html  css  js  c++  java
  • java反射中Method类invoke方法的使用方法

    package com.zsw.test;

    import java.lang.reflect.Method;
    import java.lang.reflect.InvocationTargetException;

    /**
     * <p>Title: java反射中Method类invoke方法的使用方法</p> *
     * <p>Description: </p> *
     * <p>Copyright: Copyright (c) 2009</p> *
     * <p>Company: 嘉盈公司</p> *
     * @author zsw
     * @version 1.0
     */
    public class InvokeTester {
       
        private String name;
       
        public void setName(String name){
            this.name = name;
        }   
        public String getName(){
            return name;
        }   
       
        public InvokeTester() {
        }

        public int add(int param1, int param2) {
            return param1 + param2;
        }
        public String echo(String mesg) {
            return "echo" + mesg;
        }

        public static void main(String[] args) {
            Class classType = InvokeTester.class;
            try {
                Object invokertester = classType.newInstance();   //1

                Method addMethod = classType.getMethod("add", new Class[] {  //2
                        int.class, int.class
                });

                Object result = addMethod.invoke(invokertester, new Object[] {        //3
                                                 new Integer(100), new Integer(200)
                });

                System.out.println(result);
                //////////////////////
                Method echo = classType.getMethod("echo", new Class[] {String.class});
                Object obj = echo.invoke(invokertester,
                                         new Object[] {new String("jy is very good!!!")});
                System.out.println(obj.toString());
               
                ////////////////////
                InvokeTester test = new InvokeTester();   //1
                test.setName("黄翊");                      //2
               
                //Method[] methods;
                Method[] methods = test.getClass().getDeclaredMethods();  //3
               
                //循环查找获取id方法,并运行查看是否有返回值
                    for (int i = 0; i < methods.length; i++) {
                       
                            //假设此方法有get和Idkeyword则运行
                            if (methods[i].getName().indexOf("get") != -1 && methods[i].getName().indexOf("Name") != -1) {
                                    try{
                                            // 获取此get方法返回值,推断是否有值,假设没有值说明即将运行的操作新增
                                            if (methods[i].invoke(test, null) == null) {  //4                                           
                                                System.out.println("此对象没有值!!!");                            
                                            } else {
                                                   Object strName =  methods[i].invoke(test, null);
                                                   System.out.println(strName);
                                            }
                                    }catch(Exception e){
                                            System.out.print("");
                                    }
                            }
                    }
            } catch (IllegalAccessException ex) {
            } catch (InstantiationException ex) {
            } catch (SecurityException ex) {
            } catch (NoSuchMethodException ex) {
            } catch (InvocationTargetException ex) {
            } catch (IllegalArgumentException ex) {
            }
        }
    }

  • 相关阅读:
    吃货联盟订餐系统
    第一章课后习题
    hostapd阅读(openwrt)-1
    通过C语言获取MAC地址(转)
    OpenWrt 编译分割
    ubuntu 12.04无盘工作站
    WEB前端性能优化-如何提高页面加载速度
    HTML6
    easyui 根据值查找匹配
    收藏的网站
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4280149.html
Copyright © 2011-2022 走看看