java常用的几个反射方法

db一张table里存了 1列是表名,1列是column名
要求根据查出来的表明反射到对应的class上,然后取column值
因为class名和方法名都不固定,所以只能用反射
方法的反射
https://blog.csdn.net/u013361445/article/details/49997693

// 带参数 protected Object invokeMethod(Object owner, String methodName ,Object[] args) {Class ownerClass = owner.getClass(); Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); } Object result = null; try { Method method = ownerClass.getMethod(methodName,argsClass); result = method.invoke(owner, args); } catch (Exception e) {}return result; } // 不带参数 protected String invokeMethod(Object owner, String methodName ) {Class ownerClass = owner.getClass(); String result = null; try { Method method = ownerClass.getMethod(methodName); result = String.valueOf(method.invoke(owner)); } catch (Exception e) {}return result; }

【java常用的几个反射方法】类的反射
https://blog.csdn.net/panda1234lee/article/details/9009719
/*通过Constructor.newInstance()创建新的类示例*/ private void newInstanceByConstructorNewInstance(){ try {/*可以使用相对路径,同一个包中可以不用带包路径*/ Class c=Class.forName("A"); /*以下调用无参的、私有构造函数*/ Constructor c0=c.getDeclaredConstructor(); c0.setAccessible(true); A a0=(A)c0.newInstance(); /*以下调用带参的、私有构造函数*/ Constructor c1=c.getDeclaredConstructor(new Class[]{int.class,int.class}); c1.setAccessible(true); A a1=(A)c1.newInstance(new Object[]{5,6}); } catch (Exception e) { e.printStackTrace(); } }

    推荐阅读