黑马程序员_java基础加强之JavaBean

来源:互联网 发布:coc胖子升级数据 编辑:程序博客网 时间:2024/06/04 17:49
------- android培训java培训、期待与您交流! ----------

内省 JavaBean

jJavaBean是一种特殊的Java类,主要用于传递数据信息,这种java类中的方法主要用于访问私有的字段,而且方法名符合某种命名规则。

先决条件必须有 setter getter方法用来设置和获取对象的属性值:

setter方法没有返回值,getter有返回值。符合这么一个规范,

如下面的例子:

class Person {

private int age;

public int getAge() {

return this.age;

}

public void setAge(int age) {

this.age = age;

}

}

JavaBean的属性是有类的属性来看的。

JavaBean方法的命名规则:

Field命名规则

Age -->如果第二个字母是小写,则把第一个字母变小写-->age

CPU-->如果第二个字么是大写,则不变-->CPU

方法命名规则 

Field命名规则一致

gettime -->time

getTime-->time

getCPU -->CPU

使用JavaBean的好处:

JavaEE开发中,经常要使用到JavaBean,很多环境就要求按JavaBean方式进行操作。

内省方式操作JavaBean

简单的内省操作:

定义一个类Point 成员变量为int类型的xy,被private修饰,提供setget方法,提供point的显示构造函数

public class Point {

private int x ;

private int y;

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

Point(){}

public Point(int x, int y) {

super();

this.x = x;

this.y = y;

}

}

public class JavaBean {

public static void main(String[] args) throws Exception{

Point p = new Point(3,5);

String propertyName = "x";

//反射方法 x-->X-->getX-->MethodGetX

// JavaBean 方法

Object x = getProperty(p,propertyName);

System.out.println(x);

setProperty(p, propertyName, 7);

System.out.println(p.getX());

}

private static void setProperty(Point p, String propertyName, Object obj) throws Exception

//获取 对象p 字段为propertyName的属性   与反射的第一步类似 

PropertyDescriptor pd = new PropertyDescriptor(propertyName,p.getClass());

// 获取propertyName的set方法  与反射的getMethod 类似

Method methodSetX = pd.getWriteMethod();

//执行 此方法 改变p 中propertyName的值

methodSetX.invoke(p, obj);

}

private static Object getProperty(Object obj, String propertyName)

throws IntrospectionException, IllegalAccessException,

InvocationTargetException {

//通过内省 获取obj的javaBean信息

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());

//获取obj对象其成员变量的属性

PropertyDescriptor[] prop = beanInfo.getPropertyDescriptors();

//遍历查找对应propertyName属性 并获取其get方法

for(PropertyDescriptor tmp:prop){

//获取对应的propertyName 属性

if(tmp.getName().equals(propertyName)){

//获取其 get方法 

Method methodGetX = tmp.getReadMethod();

return methodGetX.invoke(obj, null);

}

}

return null;

}

}

打印结果:

3

7

通过BeanUtils操作:

public class JavaBean {

public static void main(String[] args) throws Exception{

Point p = new Point(3,5);

//BeanUtils 工具操作

System.out.println(BeanUtils.getProperty(p,"x"));

BeanUtils.setProperty(p, "x""1222");

System.out.println(p.getX());

}

}

打印结果:

3

1222

BeanUtils的高级应用:

public class exam{

private Date birthday = null;

public Date getBirthday() {

return birthday;

}

public void setBirthDay(Date birthday) {

this.birthday = birthday;

}

}

public class JavaBean {

public static void main(String[] args) throws Exception{

exam t = new exam();

Date d = new Date();

d.setTime(1111);

t.setBirthDay(d);

System.out.println(d);

System.out.println(t.getBirthday());

BeanUtils.setProperty(t, "birthday.time""111l");

System.out.println(BeanUtils.getProperty(t, "birthday.time"));

System.out.println(t.getBirthday());

}

}

可以通过BeanUtils工具包访问类似Date这种引用类型的属性,通过.来向下查询。前提只能操作有getterseter方法的变量的。

exam必须是public修饰的。

------- android培训java培训、期待与您交流! ----------