2022年JAVA的内省机制与反射机制 .pdf

上传人:Che****ry 文档编号:27253407 上传时间:2022-07-23 格式:PDF 页数:5 大小:41.77KB
返回 下载 相关 举报
2022年JAVA的内省机制与反射机制 .pdf_第1页
第1页 / 共5页
2022年JAVA的内省机制与反射机制 .pdf_第2页
第2页 / 共5页
点击查看更多>>
资源描述

《2022年JAVA的内省机制与反射机制 .pdf》由会员分享,可在线阅读,更多相关《2022年JAVA的内省机制与反射机制 .pdf(5页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、JAVA的内省机制( introspector)与反射机制( reflection)2010-05-05 20:49 相对而言, 反射比内省更容易理解一点。用一句比较白的话来概括, 反射就是让你可以通过名称来得到对象 ( 类,属性,方法 ) 的技术,这种技术比内省机制使用范围更广泛。例如我们可以通过类名来生成一个类的实例;知道了方法名,就可以调用这个方法; 知道了属性名就可以访问这个属性的值。内省是 Java 语言对 Bean 类属性、事件的一种缺省处理方法。 例如类 A 中有属性 name, 那我们可以通过 getName,setName 来得到其值或者设置新的值。通过getName/set

2、Name 来访问 name 属性,这就是默认的规则。 Java 中提供了一套API 用来访问某个属性的 getter/setter 方法, 通过这些 API 可以使你不需要了解这个规则 (但你最好还是要搞清楚) ,这些 API 存放于包 java.beans 中。一般的做法是通过类 Introspector 来获取某个对象的 BeanInfo 信息,然后通过 BeanInfo 来获取属性的描述器 ( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后我们就可以通过反射机制来调用这些方法。 下面我们来看一个例子, 这个例子

3、把某个对象的所有属性名称和值都打印出来:Java 代码1. package MyTest; 2. publicclass bean 3.private String id = null ; 4.private String name = null ; 5.6.public String getId() 7.return id; 8. 9.publicvoid setId(String id) 10. this .id = id; 11. 12. 13. public String getName() 14. return name; 15. 16. publicvoid setName(Str

4、ing name) 17. this .name = name; 18. 19. 20. 21. package MyTest; 22. import java.beans.BeanInfo; 23. import java.beans.EventSetDescriptor; 24. import java.beans.Introspector; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 5 页 - - - - - - - - - 25. import java.b

5、eans.MethodDescriptor; 26. import java.beans.PropertyDescriptor; 27. import java.lang.reflect.Method; 28. publicclass myBeanIntrospector 29. public myBeanIntrospector() 30. 31. try32. 33. / 实例化一个 Bean 34. bean beanObj = new bean(); 35. / 依据 Bean产生一个相关的BeanInfo 类36. BeanInfo bInfoObject = 37. Introsp

6、ector.getBeanInfo(beanObj.getClass(),beanObj.getClass().getSuperclass(); 38. / 定义一个用于显示的字符串39. String output = ; 40. 41. / 开始自省42. 43. /* 44. * BeanInfo.getMethodDescriptors() 45. * 用于获取该 Bean中的所有允许公开的成员方法,以MethodDescriptor数组的形式返回46. * 47. * MethodDescriptor类48. * 用于记载一个成员方法的所有信息49. * MethodDescript

7、or.getName() 50. * 获得该方法的方法名字51. * MethodDescriptor.getMethod() 52. * 获得该方法的方法对象( Method 类)53. * 54. * Method类55. * 记载一个具体的的方法的所有信息56. * Method.getParameterTypes() 57. * 获得该方法所用到的所有参数,以Class 数组的形式返回58. * 59. * Class.getName() 60. * 获得该类型的名字61. */62. output = 内省成员方法: n ; 63. MethodDescriptor mDescArr

8、ay = bInfoObject.getMethodDescriptors(); 64. for ( int i= 0;i0) 75. 76. / 获得参数的类型的名字77. methodParams = parameters0.getName(); 78. for ( int j= 1;jparameters.length ;j+ ) 79. 80. methodParams = methodParams + , + parametersj.getName(); 81. 82. 83. output += methodName + ( + methodParams + )n ; 84. 85

9、. System.out.println(output); 86. 87. /* 88. * BeanInfo.getPropertyDescriptors() 89. * 用于获取该 Bean中的所有允许公开的成员属性,以PropertyDescriptor数组的形式返回90. * 91. * PropertyDescriptor类92. * 用于描述一个成员属性93. * 94. * PropertyDescriptor.getName() 95. * 获得该属性的名字96. * 97. * PropertyDescriptor.getPropertyType() 98. * 获得该属性的

10、数据类型,以Class 的形式给出99. * 100. */101. output = 内省成员属性: n ; 102. PropertyDescriptor mPropertyArray = bInfoObject.getPropertyDescriptors(); 103.for ( int i= 0;imPropertyArray.length ;i+ ) 104. 105. String propertyName = 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共

11、 5 页 - - - - - - - - - mPropertyArrayi.getName(); 106. Class propertyType = mPropertyArrayi.getPropertyType(); 107. output += propertyName + ( + propertyType.getName() + )n; 108. 109. System.out.println(output); 110.111.112./* 113. * BeanInfo.getEventSetDescriptors() 114. * 用于获取该 Bean中的所有允许公开的成员事件,以

12、EventSetDescriptor数组的形式返回115. * 116. * EventSetDescriptor类117. * 用于描述一个成员事件118. * 119. * EventSetDescriptor.getName() 120. * 获得该事件的名字121. * 122. * EventSetDescriptor.getListenerType() 123. * 获得该事件所依赖的事件监听器,以Class 的形式给出124. * 125. */126. output = 内省绑定事件: n ; 127. EventSetDescriptor mEventArray = bInf

13、oObject.getEventSetDescriptors(); 128.for ( int i= 0;imEventArray.length ;i+ ) 129. 130. String EventName = mEventArrayi.getName(); 131. Class listenerType = mEventArrayi.getListenerType(); 132. output += EventName + ( + listenerType.getName() + )n; 133. 134. System.out.println(output); 135. System.

14、out.println(write by esonghui :); 136.137. 138.catch (Exception e) 139. 140. System.out.println( 异常: + e); 141. 142. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 5 页 - - - - - - - - - 143.publicstaticvoid main(String args) 144. 145.new myBeanIntrospector(); 146. 147. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 5 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com