在JAVA中如何简便的给规律的NAME赋上值??

来源:百度知道 编辑:UC知道 时间:2024/06/19 21:59:35
假如现在有
text1
text2
text3
text4
text5
要给它们赋值
form.setText1("text1");
form.setText2("text2");
form.setText3("text3");
form.setText4("text4");
form.setText5("text5");

有没有简便的方法?
zenkill 你错了
form.setText2
form.setText3
form.setText4
form.setText5呢???

你需要使用到下面几个类
java.beans.BeanInfo;
java.beans.Introspector;
java.beans.IntrospectionException;
java.beans.PropertyDescriptor;
//bean
public class Text {
public Text() {
}

private String text1;
private String text2;
public String getText1() {
return text1;
}

public void setText1(String text1) {
this.text1 = text1;
}

public void setText2(String text2) {
this.text2 = text2;
}

public String getText2() {
return text2;
}

}

//赋值类
public class BeanHandle {
public BeanHandle() {
}

public static void main(String[] args){
//实例化要注入信息的类
Text text = new Text();
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(text.getClass());
}
catch (IntrospectionException e) {
}