现在的位置: 首页 > 综合 > 正文

黑马程序员_<<基础加强--javaBean>>

2018年09月16日 ⁄ 综合 ⁄ 共 5607字 ⁄ 字号 评论关闭

--------------------ASP.Net+Android+IOS开发.Net培训、期待与您交流! --------------------

1.  javaBean

     1.概述

            使用的是get和set来表示,要是使用javaBean类来操作类,那么其属性必须符合其命名规则。

       命名规则:如果属性名第一个字符是小写,则把其变成大写

       例如:ab    getAb(){};

           如果第一个字母是大写,则后面的都不变

        例如:Abc  getAbc();

       用工具类的话,可以自动生成

使用起来:简单,方便

 

     2.PropertyDescriptor

         

        

   import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
/*符合javaBean 命名规则的类*/
class BeanDemo{
  private String name;
  public BeanDemo(String name) {
    super();
    this.name = name;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
 
}
public class javaBeanDemo {
  public static void main(String[] args) throws Exception{
    BeanDemo bd=new BeanDemo("zhangsan");
    /**
     * 使用PropertyDescriptor来导出属性或者写入属性值
     * public MethodgetReadMethod()读取属性值
     *  Method getWriteMethod()获得应该用于写入属性值
     */
    String propertyName="name";//表示的属性名称
    /*读取*/
    Object value=getProperty(bd, propertyName);
    System.out.println("name="+value);
    /*写入*/
    String newName="lisi";
    setProperty(bd, propertyName, newName);
     value=getProperty(bd,propertyName);
    System.out.println("name="+value);
  }
/*设置属性*/
  private static void setProperty(Object bd, String propertyName,
      String newName) throws IntrospectionException,
      IllegalAccessException, InvocationTargetException {
    PropertyDescriptor pd=new PropertyDescriptor(propertyName, bd.getClass());
    Method SetName=pd.getWriteMethod();//获得写入的方法
    SetName.invoke(bd,newName);//调用方法,赋值新的
  }
/*获得属性*/
  private static Object getProperty(Object bd,
      String propertyName) throws IntrospectionException,
      IllegalAccessException, InvocationTargetException {
    PropertyDescriptor pd=new PropertyDescriptor(propertyName,bd.getClass());
    Method GetName=pd.getReadMethod();//读取获得的属性
     Objectvalue=GetName.invoke(bd);//调用此方法,获取值
    return value;
  }
}
 
结果:
name=zhangsan
name=lisi

     3.BeanInfo

         举一个例子,获取值,利用循环。



public class javaBeanDemo {
  public static void main(String[] args) throws Exception {
    BeanDemo bd = new BeanDemo("zhangsan");
    BeanInfo bi = Introspector.getBeanInfo(bd.getClass());
    PropertyDescriptor[] pds=bi.getPropertyDescriptors();//获得PropertyDescriptor数组
    for(PropertyDescriptor pd:pds){
      System.out.println(pd.getName()+"="+pd.getReadMethod().invoke(bd));
    }
  }
}

     4.BeanUtil

               要是使用此类,必须导入两个jar包,

           commons-beanutils.jar和commons-logging.jar,

            BeanUtils里面静态方法,操作javaBean类的话,都是当作String类型操作的,对其赋值或者是取值都是当作String 类型操作的。

       

  importorg.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
 
public class Person {
  private String name;
  private int age;
 
  public Person(String name, int age) {
    super();
    this.name = name;
    this.age = age;
  }
 
  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 class BeanUtilDemo {
  public static void main(String[] args) throws Exception {
    Person1 per = new Person1(23, "zhansgan");
    System.out.println("使用BeanUtils操作--------------------------------");
    BeanUtils.setProperty(per, "age", 20);// 设置属性的值
    String value = BeanUtils.getProperty(per, "age");// 获取属性的值
    System.out.println(value);
    System.out.println("操作类型:"
         + BeanUtils.getProperty(per, "age").getClass().getName());
      }
 
}
结果:
使用BeanUtils操作--------------------------------
20
操作类型:java.lang.String

从结果可以看出:BeanUtils来操作类,不管属性是什么类型的,都是以String类型来操作的,

 Person类和测试类一定要分来写,不要写在提个文件中

     5.PropertyUtils

      就是把BeanUtils改成PropertyUtils即可。

   PropertyUtils操作的什么类型,赋值和获得的值的类型都一样。

       

public class BeanUtilDemo {
  public static void main(String[]args) throws Exception {
 
System.out.println("使用property操作--------------------------------");
    PropertyUtils.setProperty(per, "name", "李四");
    System.out.println(PropertyUtils.getProperty(per,"name"));
    System.out.println("操作name的类型是:"
         + PropertyUtils.getProperty(per, "name").getClass().getName());
    PropertyUtils.setProperty(per, "age", 25);
    System.out.println(PropertyUtils.getProperty(per,"age"));
    System.out.println("操作age的类型是:"
         + PropertyUtils.getProperty(per, "age").getClass().getName());
}
}
 
结果:
 
使用property操作--------------------------------
李四
操作name的类型是:java.lang.String
25
操作age的类型是:java.lang.Integer

 

PropertyUtils类来操作的根据属性的类型而定,如果属性的类型是String类型,那么就一String类型来操作,如果属性是int类型,那么就以Integer类型来操作。




综合起来操作:

public class Person {
	private String name;
	private int age;

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	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;
	}

}
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

public class BeanText {
	public static void main(String[] args) throws Exception {		
		Person per = new Person("zhansgan", 23);
		System.out.println("使用BeanUtils操作--------------------------------");
		BeanUtils.setProperty(per, "age", 20);//设置属性的值
		String value = BeanUtils.getProperty(per, "age");//获取属性的值
		System.out.println(value);
		System.out.println("操作类型:"+BeanUtils.getProperty(per, "age").getClass().getName());
		System.out.println("使用property操作--------------------------------");
		PropertyUtils.setProperty(per, "name", "李四");
		System.out.println(PropertyUtils.getProperty(per, "name"));
		System.out.println("操作name的类型是:"+PropertyUtils.getProperty(per, "name").getClass().getName());
		PropertyUtils.setProperty(per, "age", 25);
		System.out.println(PropertyUtils.getProperty(per, "age"));
		System.out.println("操作age的类型是:"+PropertyUtils.getProperty(per, "age").getClass().getName());
		
	}

}
结果:
使用BeanUtils操作--------------------------------
20
操作类型:java.lang.String
使用property操作--------------------------------
李四
操作name的类型是:java.lang.String
25
操作age的类型是:java.lang.Integer

--------------------ASP.Net+Android+IOS开发.Net培训、期待与您交流!
--------------------

抱歉!评论已关闭.