Java - 枚举
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java - 枚举,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5374字,纯文字阅读大概需要8分钟。
内容图文
Java中,枚举的其中用法:
1、定义常量:
1 /** 2 * 枚举第一种用法:常量;替代之前的定义在类中的常量 3 */ 4 package enum_; 5 6 public enum Color1 7 { 8 RED, 9 YELLOW, 10 BLUE, 11 GREEN // 最后一个加逗号,加分号,不加,都没报错12} 1314class EnumDemo01 15{ 16publicstaticvoid main(String[] args) 17 { 18 Color1 color = Color1.RED; 19 Color1 color2 = Color1.RED; 20 Color1 color3 = Color1.YELLOW; 2122// 结果返回false23 System.out.println("RED".equals(color)); 24// 结果返回true25 System.out.println("RED".equals(color.toString())); 2627// 结果返回false28 System.out.println("RED".equals(Color1.RED)); 29// 结果返回true30 System.out.println("RED".equals(Color1.RED.toString())); 3132// enum类型转为String类型的两种方式 33// 1.调用toString()方法,返回此枚举常量的名称;34 String red = color.toString(); 35 System.out.println("RED".equals(red)); // 结果返回true 36// 2.调用name()方法,返回此枚举常量的名称;37 String name = color.name(); 38 System.out.println("RED".equals(name)); // 结果返回true 3940// 使用 == 或 equals() 比较两个枚举类是否相等,效果是一样的 41// 1.使用 == 进行比较42 System.out.println(color.equals(color2)); 43 System.out.println(color == color2); 44// 2.使用 equals() 进行比较45 System.out.println(color.equals(color3)); 46 System.out.println(color == color3); 47 } 48 }
2、在 switch 中使用:
1 /** 2 * 枚举的第二种用法:放在switch中 3 */ 4 package enum_; 5 6 public enum Color2 7 { 8 RED, GREEN, YELLOW, BLUE 9 } 10 11 class EnumDemo02 12 { 13 public static void main(String[] args) 14 { 15 Color2 signal = Color2.BLUE; 1617switch(signal) 18 { 19case RED: 20 System.out.println("红灯亮..."); 21break; 22case GREEN: 23 System.out.println("绿灯亮..."); 24break; 25case YELLOW: 26 System.out.println("黄灯亮..."); 27break; 28default: 29 System.out.println("灯坏了,不亮了..."); 30 } 31 } 32 }
3、添加属性和方法:
1 /** 2 * 枚举的第三种用法:向枚举中添加属性和方法 3 */ 4 package enum_; 5 6 public enum Color3 7 { 8 // 如果自定义自己的方法,那么必须在enum实例序列的最后添加一个分号;而且 Java 要求必须先定义 enum 实例; 9 // 下面实例是依据构造方法来实现这个写法 10 RED("红色",1), YELLOW("黄色",2), BLUE("蓝色",3), GREEN("绿色",4); 1112// 成员变量 13private String name; 14private Integer index; 1516// 构造方法 17// 构造方法的访问限定符只能是private或默认不写18private Color3(String name, Integer index) 19 { 20this.name = name; 21this.index = index; 22 } 2324// 普通方法 25public String getColorName(Integer index) 26 { 27for(Color3 c : Color3.values()) 28 { 29if(c.getIndex() == index) 30return c.getName(); 31 } 3233returnnull; 34 } 3536// get、set 方法 37public String getName() 38 { 39return name; 40 } 41publicvoid setName(String name) 42 { 43this.name = name; 44 } 45public Integer getIndex() 46 { 47return index; 48 } 49publicvoid setIndex(Integer index) 50 { 51this.index = index; 52 } 5354} 5556class EnumDemo03 57{ 58publicstaticvoid main(String[] args) 59 { 60 Color3 c = Color3.RED; 61 System.out.println(c.getName()); 62 System.out.println(c.getIndex()); 63 System.out.println(c.getColorName(2)); 64 } 65 }
4、覆盖枚举的方法:
1 /** 2 * 枚举第四种用法:覆盖枚举的方法 3 */ 4 package enum_; 5 6 public enum Color4 7 { 8 RED("红色",1), YELLOW("黄色",2), BLUE("蓝色",3), GREEN("绿色",4); 910private String name; 11private Integer index; 1213private Color4(String name, Integer index) 14 { 15this.name = name; 16this.index = index; 17 } 1819public String getName() 20 { 21return name; 22 } 23publicvoid setName(String name) 24 { 25this.name = name; 26 } 27public Integer getIndex() 28 { 29return index; 30 } 31publicvoid setIndex(Integer index) 32 { 33this.index = index; 34 } 3536// 重写toString()方法37 @Override 38public String toString() 39 { 40return "name:" + name + ",index:" + index; 41 } 4243} 4445class EnumDemo04 46{ 47publicstaticvoid main(String[] args) 48 { 49 Color4 c = Color4.RED; 50 System.out.println(c.getName()); 51 System.out.println(c.getIndex()); 52 System.out.println(c.toString()); 53 } 54 }
5、实现接口:
1 /** 2 * 枚举第五种用法:实现接口 3 */ 4 package enum_; 5 6 public enum Color5 implements Behaviour 7{ 8 RED("红色",1), YELLOW("黄色",2), BLUE("蓝色",3), GREEN("绿色",4); 910private String name; 11private Integer index; 1213private Color5(String name, Integer index) 14 { 15this.name = name; 16this.index = index; 17 } 1819 @Override 20publicvoid printColorInfo() 21 { 22 System.out.println("name:" + name + ",index:" + index); 23 } 2425public String getName() 26 { 27return name; 28 } 29publicvoid setName(String name) 30 { 31this.name = name; 32 } 33public Integer getIndex() 34 { 35return index; 36 } 37publicvoid setIndex(Integer index) 38 { 39this.index = index; 40 } 4142 @Override 43public String toString() 44 { 45return "name:" + name + ",index:" + index; 46 } 4748} 4950interface Behaviour 51{ 52void printColorInfo(); 53} 5455class EnumDemo05 56{ 57publicstaticvoid main(String[] args) 58 { 59 Color5 c = Color5.RED; 60 System.out.println(c.toString()); 61 c.printColorInfo(); 62 } 63 }
6、使用接口组织枚举:
1 /** 2 * 枚举第六种用法:使用接口组织枚举 3 */ 4 package enum_; 5 6 // 还必须导包 7 import enum_.Color6.Color6_1; 8 import enum_.Color6.Color6_2; 9 10 public interface Color6 11 { 12 enum Color6_1 13 { 14 RED, YELLOW 15 } 16 17 enum Color6_2 18 { 19 BLUE, GREEN 20 } 21 } 22 23 class EnumDeno06 24 { 25 public static void main(String[] args) 26 { 27 Color6_1 c1 = Color6.Color6_1.RED; 28 Color6_2 c2 = Color6.Color6_2.BLUE; 2930 System.out.println(c1.name()); 31 System.out.println(c2.name()); 32 } 33 }
7、枚举集合的使用:
java.util.EnumSet和java.util.EnumMap是两个枚举集合;EnumSet保证集合中的元素不重复;
EnumMap中的key是enum类型,而value则可以是任意类型;关于这个两个集合的使用就不在这里赘述,可以参考JDK文档;
原文:https://www.cnblogs.com/kehuaihan/p/8455859.html
内容总结
以上是互联网集市为您收集整理的Java - 枚举全部内容,希望文章能够帮你解决Java - 枚举所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。