惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - fengye515

开发常用工具 Qt的内存管理 pyqt4制作透明无边框窗体 面向对象 Java线程学习和总结 JVM,反射与动态代理 JDK5新特性之二----新的格式化输出 JDK5新特性之一----静态导入 第五章Java虚拟机(Chapter Five: The Java Virtual Machine) Java虚拟机体系结构概述 Java虚拟机 面向对象的思维方法 JDK5.0的11个主要新特征 精通java的一些必要条件 简单的范型实例程序 Spring 入门 Hibernate快速入门 设计模式 用J2ME开发企业级无线应用
Java 1.5新特性Enum的用法
fengye515 · 2007-02-25 · via 博客园 - fengye515

Enum是enumeration(列举)的简写形式,包含在java.lang包中.熟悉C, C++, C#, 或 Pascal人应该对列举有所了解,先看个例子:

public enum Season { winter, spring, summer, fall }

一个enum是定义一组值的对象,它可以包括零个或多个值成员.它是属于enum类型的,一个enum对象中不可有两个或多个相同的属性或值.在次之前的java程序员一般是 用接口的方法实现列举的,如 :

public interface Season {

   static winter = 0;

   static spring = 1; //etc..

}

引入了enum的java的列举的编写方便了许多,只须定义一个enum型的对象.enum对象的值都回自动获得一个数字值,从0开始,依次递增.看一个比较简单的enum实现的例子:

EnumDemo.java

package net.javagarage.enums;

/*

We can loop over the values we put into the enum

using the values() method.

Note that the enum Seasons is compiled into a

separate unit, called EnumDemo$Seasons.class

*/

public class EnumDemo {

      /*declare the enum and add values to it. note that, like in C#, we don't use a ; to

 end this statement and we use commas to separate the values */

      private enum Seasons { winter, spring,

       summer, fall }

      //list the values

      public static void main(String[] args) {

            for (Seasons s : Seasons.values()){

                  System.out.println(s);

            }

      }

}
运行上述代码你回得到 以下结果:

winter

spring

summer

fall

Enum的属性调用:

下面的代码展示了调用enum对象的方法,这也是它通常的用法:

package net.javagarage.enums;

/*

File: EnumSwitch.java

Purpose: show how to switch against the values in an enum.

*/

public class EnumSwitch {

      private enum Color { red, blue, green }

      //list the values

      public static void main(String[] args) {

            //refer to the qualified value

            doIt(Color.red);

      }

      /*note that you switch against the UNQUALIFIED name. that is, "case Color.red:" is a

 compiler error */

      private static void doIt(Color c){

      switch (c) {

      case red: 

            System.out.println("value is " + Color.red);

            break;

      case green: 

            System.out.println("value is " + Color.green);

            break;

      case blue: 

            System.out.println("value is : " + Color.blue);

            break;

      default : 

            System.out.println("default");

      }

      }
}

为enums添加属性和方法

enums也可以象一般的类一样添加方法和属性,你可以为它添加静态和非静态的属性或方法,这一切都象你在一般的类中做的那样.

package net.javagarage.enums;

/*

File: EnumDemo.java

Purpose: show how to use an enum that also defines its own fields and methods

*/

public class EnumWithMethods {

//declare the enum and add values to it.

public enum Season {

      winter, spring, summer, fall;

      private final static String location = "Phoenix";

      public static Season getBest(){

            if (location.equals("Phoenix"))

                  return winter;

            else

                  return summer;

      }

      public static void main(String[] args) {

      System.out.println(Season.getBest());

      }

}

就是这么的简单.但是有一点是需要注意的,那就是enums的值列表必须紧跟在enum声明,不然编译时将会出错.

Enums构造函数:

和类一样enums也可以有自己的构造函数,如下:

package net.javagarage.enums;

public class EnumConstructor {

      public static void main(String[] a) {

            //call our enum using the values method

            for (Temp t : Temp.values())

                  System.out.println(t + " is : " + t.getValue());

      }

      //make the enum

      public enum Temp {

            absoluteZero(-459), freezing(32),

            boiling(212), paperBurns(451);

      //constructor here

      Temp(int value) {

            this.value = value;

      }

      //regular field?but make it final,

      //since that is the point, to make constants

      private final int value;

      //regular get method

      public int getValue() {

      return value;

      }

      }

}
输出结果是:

absoluteZero is : -459

freezing is : 32

boiling is : 212

paperBurns is : 451

尽管enums有这么多的属性,但并不是用的越多越好,如果那样还不如直接用类来的直接.enums的优势在定义int最终变量仅当这些值有一定特殊含义时.但是如果你需要的是一个类,就定义一个类,而不是enum.