import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


/**
 * @创建人 yongming.li
 * @创建时间 2019/11/11
 * @描述
 */
public class Test {
    public static class RewardRunishment{
        public static final String REWARD = "奖励";
        public static final String PUNISHMENT = "惩罚";

        public static List<String> values(){
            List<String> list = new ArrayList<>();
            Field[] fields = RewardRunishment.class.getDeclaredFields();
            for (int i=0;i<fields.length;i++) {
                try {
                    //得到属性
                    Field field = fields[i];
                    //打开私有访问
                    field.setAccessible(true);
                    //获取属性
                    String name = field.getName();
                    //获取属性值
                    Object value = field.get(RewardRunishment.class);
                    //一个个赋值
                    list.add(String.valueOf(value));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            return list;
        }
    }
}
import com.alibaba.fastjson.JSONObject;

/**
 * @author yongming.li
 * @date 2019-11-15
 * @description:测试
 * @version: 1.0$
 */
public class Test2 {
    public static void main(String[] args) {
        System.out.println(JSONObject.toJSONString(Test.RewardRunishment.values()));
    }
}

运行结果:

["奖励","惩罚"]

 

进行优化:

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yongming.li
 * @date 2019-11-15
 * @description:静态内部类的父类
 * @version: $
 */
public class TestParent {
    public static List<?> values(Class clazz){
        List<Object> list = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        for (int i=0;i<fields.length;i++) {
            try {
                //得到属性
                Field field = fields[i];
                //打开私有访问
                field.setAccessible(true);
                //获取属性
                String name = field.getName();
                //获取属性值
                Object value = field.get(clazz);
                //一个个赋值
                list.add(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


/**
 * @创建人 yongming.li
 * @创建时间 2019/11/11
 * @描述
 */
public class Test {
    public static class RewardRunishment extends TestParent{
        public static final String REWARD = "奖励";
        public static final String PUNISHMENT = "惩罚";
        public static List<String> values(){
            return (List<String>) RewardRunishment.values(Test.RewardRunishment.class);
        }
    }
    public static class OrderType extends TestParent{
        public static final Integer BOOK = 1;
        public static final Integer SHARE = 2;
        public static List<Integer> values(){
            return (List<Integer>) OrderType.values(Test.OrderType.class);
        }
    }
}
import com.alibaba.fastjson.JSONObject;

/**
 * @author yongming.li
 * @date 2019-11-15
 * @description:测试
 * @version: 1.0$
 */
public class Test2 {
    public static void main(String[] args) {
        System.out.println(JSONObject.toJSONString(Test.RewardRunishment.values()));
        System.out.println(JSONObject.toJSONString(Test.OrderType.values()));
    }
}

运行结果:

["奖励","惩罚"]
[1,2]