首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

反照小代码

2012-09-10 
反射小代码/*** 将所有属性值设置为 null* @throws SecurityException* @throws NoSuchMethodException* @

反射小代码
/**
     * 将所有属性值设置为 null
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    @SuppressWarnings("unchecked")
    public void clean() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        Class thisClass = this.getClass();
        Field[] fields = thisClass.getFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            String stringLetter = fieldName.substring(0, 1).toUpperCase();
            Method setMethod = thisClass.getMethod(
                "set" + stringLetter.substring(1) + fieldName, new Class[] { field.getType() });
            setMethod.invoke(thisClass, new Object[]{null});
        }
    }

热点排行