JavaBeanの簡単なインスタンスのデモ
1961 ワード
最初のJavaクラスを作成するには、次の手順に従います.
2番目の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;
}
}
2番目の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);
}
}
}