当前位置:天才代写 > tutorial > JAVA 教程 > Java 1.5新特性Enum的用法

Java 1.5新特性Enum的用法

2017-11-10 08:00 星期五 所属: JAVA 教程 浏览:529

副标题#e#

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


#p#副标题#e#

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声明,否则编译时将会堕落.

#p#副标题#e#

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;
}
}
}

输出功效是:

absoluteZerois:-459

freezingis:32

boilingis:212

paperBurnsis:451

尽量enums有这么多的属性,但并不是用的越多越好,假如那样还不如直接用类来的直接.enums的优势在界说int最终变量仅当这些值有必然非凡寄义时.可是假如你需要的是一个类,就界说一个类,而不是enum.

 

    关键字:

天才代写-代写联系方式