夏日炎炎,热(⊙﹏⊙)
java反射机制,其实很好用,它可以帮我们拿到java类中被private修饰的属性。let't go
私有类如下:
仔细观察,发现类中存在私有变量,私有方法,私有构造器。那么问题来了,我再其它类中如何访问私有的属性。
1.获取私有属性变量
/** * 获取单个私用属性 */ try { Field field = Person.class.getDeclaredField("age"); field.setAccessible(true); Log.e(TAG, field.getName()); } catch (NoSuchFieldException e) { e.printStackTrace(); }
这样就可以拿到某个私有属性变量名
/** * 获取全部私用属性 */ private void do1All() { Person person = new Person("李四"); Field[] fields = Person.class.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { fields[i].setAccessible(true); try { Log.e(TAG, fields[i].getName());//获取属性名 Log.i(TAG, fields[i].get(person) + "");//获取属性值 } catch (IllegalAccessException e) { Log.i(TAG, "IllegalAccessException"); e.printStackTrace(); } } }
2.调用私有方法(这个较为常用,因为这样我们可以调用第三方的私有方法)
有参的方法 Person person = new Person("李四"); Method[] methods = Person.class.getDeclaredMethods(); // Class<?> cl = null; try { // cl=Class.forName("com.hdy.text.myapplication.Person"); //需要加入参数类型 Method method = Person.class.getDeclaredMethod("speak", String.class, int.class); method.setAccessible(true); Log.i(TAG, "方法名:" + method.getName()); //调用时需要加入参数 method.invoke(person, "111", 101); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }
3.获取私有构造方法,然后创建对象
//无参构造 try { Constructor<Person> declaredConstructor = Person.class.getDeclaredConstructor(); declaredConstructor.setAccessible(true); //加不加null都行 // Person person = declaredConstructor.newInstance(); Person person = declaredConstructor.newInstance(null); person.setPersonName("张三"); Log.i(TAG, person.getPersonName()); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //无参构造2 try { Class cls = Person.class; Constructor declaredConstructor = cls.getDeclaredConstructor(); declaredConstructor.setAccessible(true); Person person = (Person) declaredConstructor.newInstance(); person.setPersonName("王五"); Log.i(TAG, person.getPersonName()); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //有参构造 try { // Constructor<Person> declaredConstructor = Person.class.getDeclaredConstructor(new Class[]{int.class}); Constructor<Person> declaredConstructor = Person.class.getDeclaredConstructor(int.class); declaredConstructor.setAccessible(true); // Person person = declaredConstructor.newInstance(new Object[]{100}); Person person = declaredConstructor.newInstance(100); Field field = Person.class.getDeclaredField("age"); field.setAccessible(true); Log.i(TAG, field.get(person) + ""); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); }
虽然觉得有点暴力,但还是挺好用的。