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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© tfy 中级黑马   /  2012-12-23 01:01  /  2831 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.itheima.base;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class RefelctDemo1 {

        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
//                test1();
                Class clazz = Class.forName("com.itheima.base.Person");
                Person p = (Person) clazz.newInstance();//调用默认的构造方法
//                Method writeMethod = clazz.getMethod("setUsername", String.class);
//                writeMethod.invoke(p, "哈哈");
//                System.out.println(p.getUsername());
                Method method = clazz.getMethod("method1", String[].class);
                method.invoke(p, (Object)new String[]{"a","b"});
                method.invoke(p, new Object[]{new String[]{"a","b"}});
               

        }

        private static void test1() throws IntrospectionException,
                        IllegalAccessException, InvocationTargetException {
                Person p=new Person();
                PropertyDescriptor pd=new PropertyDescriptor("username", Person.class);
                Method writeMethod = pd.getWriteMethod();
                writeMethod.invoke(p, "护发素");//setusername
//                System.out.println(p.getUsername());
                Method readMethod = pd.getReadMethod();
                Object invoke = readMethod.invoke(p, null);
                System.out.println(invoke);
        }

}


0 个回复

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