java中public private protected,Java PropertyDescriptor對象

 2023-12-09 阅读 37 评论 0

摘要:1、PropertyDescriptor簡述 ? ? ? PropertyDescriptor對象是位于java.beans包下的工具類,顧名思義為屬性描述器,通常我們用于通過反射獲取對象方法的時候,下面來看一下常用的用法吧! 2、PropertyDescriptor用法 (1)、給你一個java對

1、PropertyDescriptor簡述

? ? ? PropertyDescriptor對象是位于java.beans包下的工具類,顧名思義為屬性描述器,通常我們用于通過反射獲取對象方法的時候,下面來看一下常用的用法吧!

2、PropertyDescriptor用法

(1)、給你一個java對象,你如何生成PropertyDescriptor對象呢?

java中public private protected。? ? ? ? 通常,我們會用到PropertyUtilsBean對象(位于java.beans包下),代碼如下:student為我們已經獲取到的對象,此時獲取到studentDescriptors數組,打印下可以看到獲取到的對象的相關信息。

? ? ? ? PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
?? ??? ?PropertyDescriptor[] studentDescriptors = propertyUtilsBean.getPropertyDescriptors(student);
?? ??? ?System.out.println("studentDescriptors:"+JSON.toJSONString(studentDescriptors));

這里我們看一下獲取到的一個PropertyDescriptor對象的信息

{"bound": false,"constrained": false,"displayName": "id","expert": false,"hidden": false,"name": "id","preferred": false,"propertyType": "java.lang.String","readMethod": {"accessible": false,"annotatedExceptionTypes": [],"annotatedParameterTypes": [],"annotatedReceiverType": {"annotations": [],"declaredAnnotations": [],"type": "com.iflytek.zbg.zwfw.bog.situation.common.entity.BaseEntity"},"annotatedReturnType": {"annotations": [],"declaredAnnotations": [],"type": "java.lang.String"},"annotations": [],"bridge": false,"declaringClass": "com.iflytek.zbg.zwfw.bog.situation.common.entity.BaseEntity","default": false,"exceptionTypes": [],"genericExceptionTypes": [],"genericParameterTypes": [],"genericReturnType": "java.lang.String","modifiers": 1,"name": "getId","parameterAnnotations": [],"parameterCount": 0,"parameterTypes": [],"returnType": "java.lang.String","synthetic": false,"typeParameters": [],"varArgs": false},"shortDescription": "id","writeMethod": {"accessible": false,"annotatedExceptionTypes": [],"annotatedParameterTypes": [{"annotations": [],"declaredAnnotations": [],"type": "java.lang.String"}],"annotatedReceiverType": {"annotations": [],"declaredAnnotations": [],"type": "com.iflytek.zbg.zwfw.bog.situation.common.entity.BaseEntity"},"annotatedReturnType": {"annotations": [],"declaredAnnotations": [],"type": "void"},"annotations": [],"bridge": false,"declaringClass": "com.iflytek.zbg.zwfw.bog.situation.common.entity.BaseEntity","default": false,"exceptionTypes": [],"genericExceptionTypes": [],"genericParameterTypes": ["java.lang.String"],"genericReturnType": "void","modifiers": 1,"name": "setId","parameterAnnotations": [[]],"parameterCount": 1,"parameterTypes": ["java.lang.String"],"returnType": "void","synthetic": false,"typeParameters": [],"varArgs": false}
}

以上可以看到我們已經獲取到了Student對象的id屬性(包含set和get方法)相關的全部信息。

(2)、幾個常用的方法

propertydescriptor類,? ? ? ??prop.getName()? 獲取屬性編碼

? ? ? ??prop.getReadMethod()? 獲取get方法

? ? ? ??prop.getWriteMethod()? 獲取set方法

? ? ? ? prop.getPropertyType()? 獲取屬性的定義類型

(3)、當我們獲取到方法了以后,如何獲取之前對象這個屬性的值呢?

java創建一個對象,? ? ? ? 通過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固定的屬性中

java的對象是什么意思?3、實戰練習,將一個對象屬性值復制到另一個對象中。

// 學生1為獲取到有數據的對象,學生2為new出來的無值對象
// 這里student1和student2并非要求為同一個對象,屬性字段相同即可
public static void copyCommonProperties(Object student1, Object student2) {if (student1 == null || student2 == null) {return;}// 獲取student1的所有屬性及方法PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(student1);for (int i = 0; i < descriptors.length; i++) {// 獲取某一個屬性的全部信息PropertyDescriptor propItem = 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;}// 值復制。調用寫方法,設置值// 獲取student2的當前屬性propItem.getName()的信息PropertyDescriptor prop = propertyUtilsBean.getPropertyDescriptor(student2, propItem.getName());if (null != prop && prop.getWriteMethod() != null) {prop.getWriteMethod().invoke(student2, val);}} catch (Exception e) {logger.error("復制出錯 ,student1 prop : " + propItem.getDisplayName() + " student1 class: " + student1.getClass()+ ";student2 type :" + student2.getClass(), e);}}}

學海無涯苦作舟,遇到一個,總結一個吧!以上僅為自己探討學習的理解,如有問題,歡迎大家的指導!

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/195811.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息