java利用反射原理:根据某个对象的名称和方法去执行该方法。

java利用反射原理:根据某个对象的名称和方法去执行该方法。

实现类似C语言传递函数指针可以调用函数。钩子的作用。这个可以绕过接口


不带参数的反射:


带参数的反射可参考这个:

/**  * 用反射得到系统隐藏api 读取系统属性  * @param key  * @return  */ public static String getPropertyReflect(String key) {

   // reflect call system properties  Class osSystem = null;
   try {
      osSystem = Class.forName("android.os.SystemProperties");
      Method getDeviceIDMethod = osSystem.getMethod("get",
            new Class[] { String.class });
      String tv2deviceid = (String) getDeviceIDMethod.invoke(osSystem,
            new Object[] { key });
      if (tv2deviceid != null && tv2deviceid.length() > 0) {

         Log.d(TAG, key + " = " + tv2deviceid);
         return tv2deviceid;
      }

   } catch (Exception e1) {
      e1.printStackTrace();
   }

   return "";
}

你可能感兴趣的:(java,反射)