//定义一个javaBean
public class StudentBean {
private String name;private int age;
private Boolean lb;
private double fen;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Boolean getLb() {
return lb;
}
public void setLb(Boolean lb) {
this.lb = lb;
}
public double getFen() {
return fen;
}
public void setFen(double fen) {
this.fen = fen;
}
}
//练习类
public class studentBeanTest {
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StudentBean student=new StudentBean();
BeanUtils b=new BeanUtils();
//设置属性
b.setProperty(student,"name", "张三");
b.setProperty(student,"age", "12");
b.setProperty(student,"lb", "true");
b.setProperty(student,"fen", "10.23D");
//Map集合数据存放到javaBean中。
Map<String, String> map=new HashMap<String, String>();
map.put("name", "王二");
map.put("age", "112");
map.put("lb", "false");
map.put("fen", "1.02D");
b.populate(student, map);
System.out.println(b.getProperty(student, "name"));
System.out.println(b.getProperty(student, "age"));
System.out.println(b.getProperty(student, "lb"));
System.out.println(b.getProperty(student, "fen"));
//javaBean数据存放到Map集合中。
Map<String, String> map1=new HashMap<String, String>();
map1=b.describe(student);
Set<Entry<String, String>> entry =map1.entrySet();
//将javaBean数据存放到Map集合后,迭代打印map1集合里的数据,会多出来一个(class-----class com.tsh.text.StudentBean)键值对信息
for (Entry<String, String> entry2 : entry) {
System.out.println(entry2.getKey());
System.out.println(entry2.getValue());
}
}
}