# 枚举入门

220人阅读 评论(0)

2004 年 11 月 09 日

Tiger 中的一个重要新特性是枚举构造，它是一种新的类型，允许用常量来表示特定的数据片断，而且全部都以类型安全的形式来表示。Tiger 专家、developerWorks 的多产作者 Brett McLaughlin 将解释枚举的定义，介绍如何在应用程序中运用枚举，以及它为什么能够让您抛弃所有旧的 public static final 代码。

 public class OldGrade { public static final int A = 1; public static final int B = 2; public static final int C = 3; public static final int D = 4; public static final int F = 5; public static final int INCOMPLETE = 6; } 

 package com.oreilly.tiger.ch03; public enum Grade { A, B, C, D, F, INCOMPLETE }; 

 package com.oreilly.tiger.ch03; public class Student { private String firstName; private String lastName; private Grade grade; public Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getFirstName() { return firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getLastName() { return lastName; } public String getFullName() { return new StringBuffer(firstName) .append(" ") .append(lastName) .toString(); } public void assignGrade(Grade grade) { this.grade = grade; } public Grade getGrade() { return grade; } } 

 public void listGradeValues(PrintStream out) throws IOException { for (Grade g : Grade.values()) { out.println("Allowed value: '" + g + "'"); } } 

 Allowed Value: 'A' Allowed Value: 'B' Allowed Value: 'C' Allowed Value: 'D' Allowed Value: 'F' Allowed Value: 'INCOMPLETE' 

 public void testSwitchStatement(PrintStream out) throws IOException { StringBuffer outputText = new StringBuffer(student1.getFullName()); switch (student1.getGrade()) { case A: outputText.append(" excelled with a grade of A"); break; case B: // fall through to C case C: outputText.append(" passed with a grade of ") .append(student1.getGrade().toString()); break; case D: // fall through to F case F: outputText.append(" failed with a grade of ") .append(student1.getGrade().toString()); break; case INCOMPLETE: outputText.append(" did not complete the class."); break; } out.println(outputText.toString()); } 

 public void testSwitchStatement(PrintStream out) throws IOException { StringBuffer outputText = new StringBuffer(student1.getFullName()); switch (student1.getGrade()) { case A: outputText.append(" excelled with a grade of A"); break; case B: // fall through to C case C: outputText.append(" passed with a grade of ") .append(student1.getGrade().toString()); break; case D: // fall through to F case F: outputText.append(" failed with a grade of ") .append(student1.getGrade().toString()); break; case INCOMPLETE: outputText.append(" did not complete the class."); break; default: outputText.append(" has a grade of ") .append(student1.getGrade().toString()); break; } out.println(outputText.toString()); } 

 package com.oreilly.tiger.ch03; public enum Grade { A, B, C, D, F, INCOMPLETE , WITHDREW_PASSING, WITHDREW_FAILING }; 

 package com.oreilly.tiger.ch03; public enum AntStatus { INITIALIZING, COMPILING, COPYING, JARRING, ZIPPING, DONE, ERROR } 

 public void testEnumMap(PrintStream out) throws IOException { // Create a map with the key and a String message EnumMap antMessages = new EnumMap(AntStatus.class); // Initialize the map antMessages.put(AntStatus.INITIALIZING, "Initializing Ant..."); antMessages.put(AntStatus.COMPILING, "Compiling Java classes..."); antMessages.put(AntStatus.COPYING, "Copying files..."); antMessages.put(AntStatus.JARRING, "JARring up files..."); antMessages.put(AntStatus.ZIPPING, "ZIPping up files..."); antMessages.put(AntStatus.DONE, "Build complete."); antMessages.put(AntStatus.ERROR, "Error occurred."); // Iterate and print messages for (AntStatus status : AntStatus.values() ) { out.println("For status " + status + ", message is: " + antMessages.get(status)); } } 

 [echo] Running AntStatusTester... [java] For status INITIALIZING, message is: Initializing Ant... [java] For status COMPILING, message is: Compiling Java classes... [java] For status COPYING, message is: Copying files... [java] For status JARRING, message is: JARring up files... [java] For status ZIPPING, message is: ZIPping up files... [java] For status DONE, message is: Build complete. [java] For status ERROR, message is: Error occurred. 

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：6026次
• 积分：110
• 等级：
• 排名：千里之外
• 原创：3篇
• 转载：8篇
• 译文：0篇
• 评论：0条