java 反射

来源:互联网 发布:grub修复启动到windows 编辑:程序博客网 时间:2024/04/25 08:32

    public static void main(String[] args) throws Exception{
        TbIntVsopExceptionLog log = new TbIntVsopExceptionLog();
        //字段处理
        Field f = TbIntVsopExceptionLog.class.getDeclaredField("exception_log_id");
        f.setAccessible(true);
        f.set(log, new Long(12));
        System.out.println(log.getException_log_id());//12
        System.out.println(log.getException_position());//null
       
        //方法处理
        Method setMethod = (Method) TbIntVsopExceptionLog.class
                        .getMethod("setException_log_id", new Class[] {Long.class});
        setMethod.invoke(log, new Object[] {new Long(100)});
        Method getMethod = (Method) TbIntVsopExceptionLog.class
                        .getMethod("getException_log_id", new Class[] {});
        Long l = (Long)getMethod.invoke(log, new Object[] {});
        System.out.println(log.getException_position());//null
        System.out.println(l);//100
    }

原创粉丝点击