java bean基础

来源:互联网 发布:五常大米价格 知乎 编辑:程序博客网 时间:2024/06/05 04:12

</pre>JavaBean是符合某种规范的Java组件,也就是Java类。 </h2><p>它必须满足如下规范: <strong><span style="color:blue;"><strong>1</strong></span><span style="color:blue;">)必须有一个零参数的默认构造函数</span><span style="color:blue;"><strong>2</strong></span><span style="color:blue;">)必须有</span><span style="color:blue;">get</span><span style="color:blue;">和</span><span style="color:blue;">set</span><span style="color:blue;">方法,类的字段必须通过</span><span style="color:blue;">get</span><span style="color:blue;">和</span><span style="color:blue;">set</span><span style="color:blue;"><strong>   </strong></span><span style="color:blue;">方法来访问。</span><span style="color:blue;"><strong>   </strong></span><span style="color:blue;">(</span><span style="color:blue;">get</span><span style="color:blue;">方法无参,</span><span style="color:blue;">set</span><span style="color:blue;">方法有参)</span></strong></p><p>1、什么是JavaBean和属性的读写方法?</p><p>有get或set方法就是一个属性(<strong><span style="color:red;">get</span><span style="color:red;">、</span><span style="color:red;">set</span><span style="color:red;">后面跟的内同一样则属于一个属性,不一样属于两个属性。</span></strong>),没有get set方法的就不是bean属性;另外所有类继承了Object类的<strong><span style="color:red;">getClass</span><span style="color:red;">()</span></strong>方法,所以还有一个属性<strong><span style="color:red;">class</span></strong>。</p><pre name="code" class="java">package cn.introspector;/** * introspector的实践类 * @author 许允冲 * * 代码时间:2016-3-8 下午4:12:50 */public class Person {private String name;private String sex;private int age;public String getAb(){return null;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

这个Person类有五个属性,nameagepasswordAbclass(从父类继承而来)

访问JavaBean属性的两种方式:

1.直接调用bean的setXXX或getXXX方法。

2.通过内省技术访问(java.beans包提供了内省的API),内省技术访问也提供了两种方式。

        A.通过PropertyDescriptor类操作Bean的属性

        B.通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的getter/setter 方法,然后通过反射机制来调用这些方法。

package cn.introspector;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;/** * 使用内省操作bean的属性。 *  * @author 许允冲 *  *         代码时间:2016-3-8 下午4:21:17 */public class IntrospectorDemo {public static void main(String[] args) throws IllegalAccessException,IllegalArgumentException, InvocationTargetException,IntrospectionException {IntrospectorDemo id = new IntrospectorDemo();/*id.test01();id.test02();*/id.test03();}/** * 得到bean的所有属性 */public void test01() {try {// 1、getBeanInfo() 在 Java Bean 上进行内省,了解其所有属性、公开的方法和事件。// 会把bean的所有bean属性(包括从父类继承的bean属性)和方法封装在BeanInfo对象中。BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);// 1.1、如果得到自身的属性那么,使用下面的方法beanInfo = Introspector.getBeanInfo(Person.class, Object.class);// 2、使用BeanInfo的getPropertyDescriptors()拿到属性描述器。PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();// 3、遍历拿到每一个属性描述器for (PropertyDescriptor propertyDescriptor : pds) {System.out.println(propertyDescriptor.getName());}} catch (IntrospectionException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 得到bean的单个属性 *  * @throws IntrospectionException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException */public void test02() throws IntrospectionException, IllegalAccessException,IllegalArgumentException, InvocationTargetException {// 1、得到该bean属性的属性描述器PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);// 2、通过得到的属性得到该属性对应的setxx()方法对应method对象。Method ageSet = pd.getWriteMethod();// setAge();// 3、调用Method的invoke方法,需要指定一下对象Person p = new Person();ageSet.invoke(p, 20);// 4、使用getReadMethod得到bean的getXX()方法对象Method ageGet = pd.getReadMethod();System.out.println(ageGet.invoke(p, null));// System.out.println(p.getAge());}/** * 获取当前操作对象的属性的类型 */public void test03() {try {// 1、得到属性描述器PropertyDescriptor propertyDescriptor = new PropertyDescriptor("age", Person.class);//2、getPropertyType()得到属性的类型Class cl=propertyDescriptor.getPropertyType();System.out.println(cl);} catch (IntrospectionException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}





0 0
原创粉丝点击