提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
具有构造函数和显式实例字段以及这些字段的访问器的枚举类型
import java.util.EnumSet;//来自 时 代 J a v a 公 众 号 - N o w J a v a . c o m enum Book { // declare constants of enum type Java("Java", "2015"), C("C", "2013"), Internet("Internet", "2012"), Cpp("C++", "2014"), VB("Visual Basic", "2014"), CSharp("Visual C#", "2014"); // instance fields private final String title; private final String copyrightYear; // enum constructor /* 时 代 Java - nowjava.com 提 供 */ Book(String title, String copyrightYear) { this.title = title; this.copyrightYear = copyrightYear; } // accessor for field title public String getTitle() { return title; } // accessor for field copyrightYear public String getCopyrightYear() { return copyrightYear; } } public class Main { public static void main(String[] args) { System.out.println("All books:"); // print all books in enum Book for (Book book : Book.values()) System.out.printf("%-10s%-45s%s%n", book, book.getTitle(), book.getCopyrightYear()); System.out.printf(