package com.sp.eclipse; public class demo3 { //枚举使用范例 public static void main(String[] args) { int grade = 100, grade2 = 75; String rString = Grade.getName(grade);//传入分数,输出分数说明 System.out.println("分数:"+ grade + "分----"+rString); String rString2 = Grade.getName(grade2);//传入分数,输出分数说明 System.out.println("分数:"+ grade2 + "分----"+rString2); } //枚举法定义分数等级说明 public enum Grade { A("优秀", 90, 100), B("良好", 80, 89), C("及格", 60, 79), D("不及格", 0, 59); private String name; private int min, max; //构建函数 private Grade(String name, int min, int max) { this.name = name; this.min = min; this.max = max; } //普通方法 public static String getName(int grade) { String rString ="" ; for (Grade g : Grade.values()) { if (grade >= g.getMin() && grade <= g.getMax()) { rString = g.getName() + g.toString(); } } return rString; } @Override//重写toString方法 public String toString() { return "(等级:" + getName() + "----分数区间:" + getMin() + "~" + getMax() + ")"; } //以下是字段属性读取设置 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMin() { return min; } public void setMin(int min) { this.min = min; } public int getMax() { return max; } public void setMax(int max) { this.max = max; } } }
原文:http://www.cnblogs.com/yanglin-gdqy-blog/p/4550393.html