关于javabean的学习

来源:互联网 发布:php新手入门教程 编辑:程序博客网 时间:2024/04/29 01:46

首先创建一个 person 的javabean


package com.ccit.bean;


public class Person 
{
private String name;
private String password;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}


下面对bean进行各种操作

package com.ccit.bean;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.junit.Test;


public class Demo01 
{
@Test
//操纵Bean的制定属性:Age;
public void test() throws Exception
{
Person p = new Person();
PropertyDescriptor pd = new PropertyDescriptor("age",Person.class);
//得到属性的写方法,为属性赋值
Method method = pd.getWriteMethod();
method.invoke(p, 45);

System.out.println(p.getAge());
//获取属性的值
method = pd.getReadMethod();
System.out.println(method.invoke(p, null));
}
//高级点的知识,或许当前操作的属性的类型
@Test
public void test1() throws Exception
{
Person p = new Person();
PropertyDescriptor pd = new PropertyDescriptor("age",Person.class);

System.out.println(pd.getPropertyType());
}


}

0 0