创建第一个Java类:
package com.hubin.bean;public class JavaBeanTest { private String name="貂蝉"; private String []skill={"闭月","离间"}; public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getSkill() { return skill; } public void setSkill(String[] skill) { this.skill = skill; } }
创建第二个Java类:
package com.hubin.bean;import java.beans.IntrospectionException;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class GetJavaBeanTest { /** * @param args */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub JavaBeanTest jbt=new JavaBeanTest(); String propertyName="skill"; setProperty(jbt, propertyName); getProperty(jbt, propertyName); } private static void setProperty(JavaBeanTest jbt, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException { PropertyDescriptor pd=new PropertyDescriptor(propertyName, jbt.getClass()); Method mt=pd.getWriteMethod(); //mt.invoke(jbt, (Object)new String[]{"舍身","诀别"}); mt.invoke(jbt,new Object[]{new String[]{"舍身","诀别"}}); } private static void getProperty(JavaBeanTest jbt, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException { PropertyDescriptor pd=new PropertyDescriptor(propertyName, jbt.getClass()); Method mt=pd.getReadMethod(); Object []retobj=(Object[]) mt.invoke(jbt); for(Object obj:retobj){ System.out.println(obj); } }}