java反射机制学习代码
根据
http://www.iteye.com/topic/137944
文档进行学习
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | package reflectTest; import java.lang.reflect.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; class A{ } class B extends A{ } class C extends B{ } class TestClass extends Object implements ActionListener { private int a = 3 ; public Integer b = 4 ; public TestClass(){} public TestClass( int id,String name){} public void Abc( int id,String name){} public void ActionPerformed(ActionEvent e){} } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub TestClass tc = new TestClass(); Class temp = tc.getClass(); //============================================== try { System.out.println( "反射类中所有公有的属性" ); Field[] fb = temp.getFields(); for ( int i= 0 ;i < fb.length;i++) { Class cl = fb[i].getType(); System.out.println( "fb: " +cl); } System.out.println( "反射类中所有的属性" ); Field[] fa = temp.getDeclaredFields(); for ( int i = 0 ;i < fa.length;i++) { Class cl = fa[i].getType(); System.out.println( "fa: " +cl); } System.out.println( "反射类中私有属性的值" ); Field f = temp.getDeclaredField( "a" ); f.setAccessible( true ); Integer i = (Integer)f.get(tc); System.out.println(i); //=============================== String className = temp.getName(); Constructor[] constructs = temp.getConstructors(); for ( int k = 0 ;k < constructs.length;k++) { Class[] Parametertypes = constructs[k].getParameterTypes(); System.out.print(className + "(" ); for ( int j = 0 ;j < Parametertypes.length;j++) { System.out.print(Parametertypes[j].getName()+ " " ); } System.out.println( ")" ); } //================================================================== Class[] interfaces = temp.getInterfaces(); for ( int j = 0 ;j<interfaces.length;j++) { System.out.println(interfaces[j].getName()); } Class superClass = temp.getSuperclass(); System.out.println(superClass.getName()); //================================================================= C c = new C(); Class t = c.getClass(); Class superClass1 = t.getSuperclass(); System.out.println(c); System.out.println(c.getClass()); System.out.println(c.getClass().getSuperclass()); System.out.println(c.getClass().getSuperclass().getSuperclass()); } catch (Exception e) { System.out.println(e); } } } |
运行结果如下:
反射类中所有公有的属性
fb: class java.lang.Integer
反射类中所有的属性
fa: int
fa: class java.lang.Integer
反射类中私有属性的值
3
reflectTest.TestClass()
reflectTest.TestClass(int java.lang.String )
java.awt.event.ActionListener
java.lang.Object
reflectTest.C@70dea4e
class reflectTest.C
class reflectTest.B
class reflectTest.A
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
![阿里打赏](https://files.cnblogs.com/files/itdef/ali2.bmp)
![微信打赏](https://files.cnblogs.com/files/itdef/weixin2.bmp)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步