读书人

黑马软件工程师_lt;lt;基础加强-javaBeangt;

发布时间: 2013-10-19 20:58:22 作者: rapoo

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

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


1. javaBean 1.概述

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

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

例如:ab getAb(){};

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

例如:Abc getAbc();

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

使用起来:简单,方便

2.PropertyDescriptor

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.String25操作age的类型是:java.lang.Integer



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


读书人网 >编程

热点推荐