Java枚举的使用
2014-11-05 17:03
330 查看
下面的类展示了枚举的用法
public class Client { public static void main(String[] args) { switch (Season.Time.Afternoon) { case Afternoon: System.out.println("good guy"); break; case Dawn: case Evening: case Morning: case Night: System.out.println("I fool you"); break; default: System.out.println("what are you doing?"); break; } for(Boy boy:Boy.values()){ System.out.println(boy.toString()+"-"+boy.getAge()+"-"+boy.getHeight()); } //EnumMap以enum值为key EnumMap<Month, String> map = new EnumMap<Month, String>(Month.class); map.put(Month.January, "一月"); map.put(Month.February, "二月"); map.put(Month.March, "三月"); map.put(Month.April, "四月"); map.put(Month.May, "五一"); map.put(Month.June, "六月"); map.put(Month.July, "七月"); for(Month m:map.keySet()){//Set,不重复 System.out.println(m); } for(String val:map.values()){//Collection System.out.println(val); } for(Entry<Month, String> entry:map.entrySet()){//Set System.out.println(entry.getKey()+"-"+entry.getValue()); } } public enum Month{ January,February,March,April,May,June,July } public enum Boy{//enum可以有自己的方法、属性 Zhangsan(16,1.55f), Lisi(18,1.69f), Wangwu(23,1.92f);//用分号来结尾 private final int age; private final float height; private Boy(int age,float height){//enum构造器只能是private,无法在外面创建对象 this.age = age; this.height = height; } public int getAge(){ return age; } public float getHeight(){ return height; } } private enum Season{//enum嵌套 Spring,Summer,Autumn,Winter; public enum Time{ Dawn,Morning,Afternoon,Evening,Night } } }
相关文章推荐
- 再谈在Java中使用枚举
- java的枚举enum的简单使用
- Annotation & Enum :Java中注解和枚举的使用
- java中使用枚举
- java enum(枚举)使用详解 + 总结
- Java中使用枚举
- java枚举使用详解
- java枚举使用详解
- java枚举使用总结
- 再谈在Java中使用枚举
- 高阶Java枚举类型enum使用详解
- java中Enum(枚举)的使用
- android的Paint.java中几个枚举的定义和使用
- 01-java基础加强,eclipse工具使用,快捷键,断点调试,单元测试,jdk5新特性,增强for循环,集合map,list,可变参数,枚举,反射,泛型
- Java 的枚举使用
- 再谈在Java中使用枚举
- 高阶Java枚举类型enum使用详解
- Java枚举(用Java普通类模拟枚举的实现原理及JDK枚举API使用示例)
- java 定义枚举+枚举使用
- 加强eclipse的使用技巧。可变参数。基本数据类型自动装箱和拆箱。枚举。反射。数组反射的应用。ArrayList和HashSet。JavaBean