项目中 枚举与注解的结合使用
编程技术  /  houtizong 发布于 3年前   167
public interface Behaviour { void print(); String getInfo(); } public enum Color implements Behaviour{ RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4); // 成员变量 private String name; private int index; // 构造方法 private Color(String name, int index) { this.name = name; this.index = index; } //接口方法 @Override public String getInfo() { return this.name; } //接口方法 @Override public void print() { System.out.println(this.index+":"+this.name); } }
@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface VersionSupport {public String value() default "";public int id();}
public class Survey { //问卷支持版本 private String versionSupport; private List<Question> questions;}public class Question {private QuestionType questionType;private String content;}public enum QuestionType {RADIT(1,"单选题"),CHECK_BOX(2,"多选题");private int key;private String value;private QuestionType(int key, String value){this.key = key;this.value = value;}}
/** * 版本支持注解 * @author 飞翔的小马甲 * @since 2014年9月11日 下午10:12:34 */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface VersionSupport {public String value();}public enum QuestionType {RADIT(1,"单选题"),@VersionSupport("1.1")CHECK_BOX(2,"多选题"),@VersionSupport("1.2")TEXT(3,"填空题");private int key;private String value;private QuestionType(int key, String value){this.key = key;this.value = value;}}
/** * 问卷类 * * @author 飞翔的小马甲 * @since 2014年9月11日 下午10:36:16 */public class Survey { //问卷支持版本 private String versionSupport; private List<Question> questions; public String getVersionSupport() {return versionSupport; } public void setVersionSupport(String versionSupport) {this.versionSupport = versionSupport; } public List<Question> getQuestions() {return questions; } public void setQuestions(List<Question> questions) {this.questions = questions; } public void initVersionSupport() throws Exception {float vs = 0.0f;for (Question question : questions) { Field field = QuestionType.class.getField(question .getQuestionType().name()); VersionSupport support = field.getAnnotation(VersionSupport.class); if(support == null){continue; }else{vs = Math.max(vs, Float.parseFloat(support.value())); }}versionSupport = String.valueOf(vs); } @Override public String toString() {return "Survey [versionSupport=" + versionSupport + ", questions="+ questions + "]"; } }
/** * * @author XieFeixiang * @since * @version 2014年9月12日 t上午11:22:08 */public class Client { public static void main(String[] args) {List<Question> questions = new ArrayList<Question>();questions.add(new Question(QuestionType.CHECK_BOX, "题目一"));questions.add(new Question(QuestionType.TEXT, "题目二"));questions.add(new Question(QuestionType.RADIT, "题目三"));Survey survey = new Survey();survey.setQuestions(questions);try { survey.initVersionSupport();} catch (Exception e) { e.printStackTrace();}System.out.println(survey); }}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接