关于反射的使用

Class xlass = Class.forName(classPath);

            System.out.println(xlass.getName());
            for(Field method : xlass.getDeclaredFields()){
                System.out.println(method.getName());

            }

            Constructor<ReflectionClass> crc = xlass.getDeclaredConstructor(String.class);
            crc.setAccessible(true);//可访问调用私有成员
            ReflectionClass rc = crc.newInstance("One World One Ghost");
            *//*Class[] cArr = {String.class,int.class,boolean.class};
            Method method = xlass.getMethod("test",cArr);
            Object[] oArr = {"NOTO",123,true};
            method.invoke(null,oArr);*//*


            Method m = xlass.getMethod("test", String.class, int.class, boolean.class);
            m.invoke(ReflectionClass.class,"HHH",333,false);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

猜你喜欢

转载自blog.csdn.net/weixin_44702017/article/details/89420175