Android 中利用反射技术实现加减乘除
2010-08-17 11:00:00 来源:WEB开发网// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
};
/**
* 操作方法
*
* @param oper
* @param first
* @param second
* @return
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
String operation(String oper, int first, int second)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException, SecurityException, NoSuchMethodException,
IllegalArgumentException, InvocationTargetException {
// 获取相应的类对象名称
Class classType = Class.forName("com.terry.operationClass");
// 如果知道类名并且类名存在于我们工程中,即jar 文件中包含可以使用如下写法
//Class classType = operationClass.class;
// 返回本类对象
Object invokeOperation = classType.newInstance();
if (oper.equals("+")) {
// 根据类对象名称去查找对应的方法
Method addMethod = classType.getMethod("add", new Class[] {
更多精彩
赞助商链接