首页 > 编程知识 正文

java对象的定义,defineproperty 对象的属性

时间:2023-05-04 01:29:31 阅读:107776 作者:1232

1、属性描述符概述

PropertyDescriptor对象是java.beans包下的工具类,顾名思义是属性描述符。 通常在通过反射获取对象方法时使用。 让我们来看看一般的使用方法。

2、属性描述器的使用方法

)1)、给你一个java对象。 如何生成PropertyDescriptor对象?

通常,使用java.beans包下的PropertyUtilsBean对象。 代码如下。 student是已经获取的对象,此时获取studentDescriptors数组并打印有关获取的对象的信息。

propertyutilsbeanpropertyutilsbean=newpropertyutilsbean (;

property descriptor [ ] student descriptors=propertyutilsbean.getproperty descriptors;

system.out.println (student descriptors : ) JSON.tojsonstring (student descriptors );

现在,我们来看一下获取的PropertyDescriptor对象的信息

{'bound': false,' constrained': false,' displayName': 'id ',' expert': false,' hidden ' 3360 false ' property type ' : ' Java.lang.string ',' read method ' : { ' accessible ' 3360 false, ' annotatedexceptiontypes ' ' annotatedreceivertype ' : { ' annotations ' : [ ],' declaredAnnotations': [], ' type ' : ' com.iflytek.zbg.zwfw.bog.situation.com mon.entity.base entity ' },' annotatedreturntype'3333 declaredAnnotations': [],' type': 'java.lang.String'} ' ',' annotations': [], ' bridge ' : faaar ' declaring class ' 3360 ' com.iflytek.zbg.zwfw.bog.situation.com mon.entity.base entity ' ' ' default ' 3360 faaa ase ' genericexceptiontypes ' : [ ],' genericParameterTypes': [], ' generic returntype ' : ' Java.lang.string ' parameter annotations ' : [ ],' parameterCount': 0,' parameteter 请参见' returntype ' 3360 ' Java.' ' type parameters ' : [ ],' varArgs': false} ',' short description ' 3360 ' ' write method ' 3360 { ' access ssion } ' annotatedexceptiontypes ' : [ ],' annotatedparametertypes ' 3360 [ { } ' declaredannotationations ' ' type ' : ' Java.lang.string ' },' annotatedreceivertype ' 3360 { ' annotations ' 3360 ' declaredannotationg ' type ' : ' com.iflytek.zbg.zwfw.bog.situation.com mon.entity.base entity ' },' annnnatek declaredAnnotations': [],' type': 'void'} ' ',' annotations': [],' bridge': false, ' declaring class ' 3360 ' com.iflytek.zbg.zwfw.bog.situation.com mon.entity.base entity ',' default ' 3360 FAA ' genericparametertypes ' : [ ' Java.lang.string ' ],' genericreturntype'3330。 ' name': 'setId ',' parameterAnnotations': [[]],' parameterCount': 1,' parameter types ' : [ ] “parameter types ' 3360”“synthetic”: false,“typeParameters': [],“varargs”3360 false }或更高版本中的Student对象

)2)、常用的几种方法

prop.getName ()获取属性代码

prop.getReadMethod ()获取get方法

prop.getWriteMethod ()获取set方法

prop.getPropertyType ()获取属性的定义类型

)3)、获取方法后,如何获取上一个对象这个属性值?

get方法通过反射对象来获取值。 代码如下所示

method method=prop.getreadmethod (; 获取//get方法

object val=method.invoke (student ); 调用student的get方法获取内容

system.out.println(val: ) JSON.tojsonstring (val );

)4)、如果需要为student的属性分配其他东西,该怎么办?

要获取set方法,必须首先验证set方法是否存在以及赋值的内容是否存在。 代码如下。

prop.getWriteMethod ().invoke ) student,val1); //student调用set方法,将val1的值设置为prop固定属性

3、实战练习,将一个对象的属性值复制到另一个对象。

//学生1获取有数据的对象,学生2获取new中没有值的对象//这里student1和student2不必是同一个对象, 如果属性字段相同,则获取publicstaticvoidcopycommonproperties (object student 1,object student1)的所有属性和方法property descriptor [ ] descriptors=propertyutilsbean.getproperty descriptors (student1); for(intI=0; i descriptors.length; I )//获取某个属性的所有信息的propertydescriptorpropitem=descriptors [ I ]; setclass/getclass属性if (' class '.equals (propitem.getname ) ) ) {continue; }try{//get方法中相应属性的值method method=propitem.getreadmethod (; object val=method.invoke (student1); //如果为空,则处理if(null==val ) {continue; //复制值。 调用方法并设置值//以获取student22的当前属性propItem.getName ()的信息propertydescriptorprop=propertyutilsbean.getproperty dean=prop prop.getWriteMethod!=null(prop.getwritemethod ) ).invoke ) student2,val ); }catch(exceptione ) {logger.error )“复印错误,student1prop : ' propitem.get display name”student1class 3360 ' stata }}让我们在学海无涯艰苦奋斗,同舟共济,相遇合一! 以上只是自己探讨学习的理解,有问题的话,欢迎大家的指导!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。