<span style="font-size:18px;">/**
* 反射机制(定义)
*
* @param owner
* @param methodName
* @param args
* @return
* @throws Exception
*/
public void invokeMethod(Object owner, String methodName, Object args[])
{
// 得到调用函数的对象(后面简称对象)所对应的Class.
Class ownerClass = owner.getClass();
// 得到函数参数所对应的Class.
Class argsClass[]= new Class[args.length];
for(int index=0; index<args.length; index++)
{
argClass[index] = args[index];
}
// 通过对象的Class和参数的Class来得到Method.
Method method = ownerClass.getMethod(methodName, argsClass);
// 利用Method和对象、函数参数来调用该方法
return method.invoke(owner, args);
}
/*
* 反射机制,setNewZoomScale这个函数不存在(原来是具有三个参数的函数)(调用)
* Object args[] = new Object[3];
* float scale = 0.5f;
* boolean textScale = true;
* boolean force = true;
* args[0] = scale;
* args[1] = textScale;
* args[2] = force;
* GameActivity.this.invokeMethod(m_webView, "setNewZoomScale", args);
*/</span>
记录反射机制
最新推荐文章于 2025-04-28 18:48:16 发布