A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

public class ReflectDemo4 {
        public static void main(String[] args) throws Exception {
                getMethodDemo_3();
        }
        public static void getMethodDemo_3() throws Exception {      
                Class clazz = Class.forName("cn.itcast.bean.Person");      
                Method method = clazz.getMethod("paramMethod", String.class,int.class);      Object obj = clazz.newInstance();
                method.invoke(obj, "小强",89);     
        }
        public static void getMethodDemo_2() throws Exception {      
                Class clazz = Class.forName("cn.itcast.bean.Person");      
                Method method = clazz.getMethod("show", null);
                //获取空参数一般方法。   
                //  Object obj = clazz.newInstance();   
                Constructor constructor = clazz.getConstructor(String.class,int.class);   
                Object obj = constructor.newInstance("小明",37);      
                method.invoke(obj, null);  
        }
        /*
         * 获取指定Class中的所有公共函数。  
         */
        public static void getMethodDemo() throws Exception {      
                Class clazz = Class.forName("cn.itcast.bean.Person");      
                Method[] methods  = clazz.getMethods();
                //获取的都是公有的方法。   
                methods = clazz.getDeclaredMethods();
                //只获取本类中所有方法,包含私有。   
                for(Method method : methods){   
                        System.out.println(method);   
                }   
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马