public enum Color{
RED,BLUE,BLACK,YELLOW,GREEN
}
显然,enum很像特殊的class,实际上enum声明定义的类型就是一个类。 而这些类都是类库中Enum类的子类(java.lang.Enum
我们可以如下定义枚举常量(实质是一个类对象)
4. public static final enum hr.test.Color RED;
5. public static final enum hr.test.Color BLUE;
6. public static final enum hr.test.Color BLACK;
7. public static final enum hr.test.Color YELLOW;
8. public static final enum hr.test.Color GREEN;
即然枚举类是class,当然在枚举类型中有构造器,方法和数据域。但是,枚举类的构造器有很大的不同:
(1) 构造器只是在构造枚举值的时候被调用。
Java代码 1.enum Color{
2. RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),YELLOW(255,255,0),GREEN(0,255,0);
3. //构造枚举值,比如RED(255,0,0)
4. private Color(int rv,int gv,int bv){
5. this.redValue=rv;
6. this.greenValue=gv;
7. this.blueValue=bv;
8. }
9.
10. public String toString(){ //覆盖了父类Enum的toString()
11. return super.toString()+"("+redValue+","+greenValue+","+blueValue+")";
12. }
13.
14. private int redValue; //自定义数据域,private为了封装。
15. private int greenValue;
16. private int blueValue;
17. }
(2) 构造器只能私有private,绝对不允许有public构造器。 这样可以保证外部代码无法新构造枚举类的实例。这也是完全符合情理的,因为我们知道枚举值是public static final的常量而已。 但枚举类的方法和数据域可以允许外部访问。
Java代码 1.public static void main(String args[])
2.{
3. // Color colors=new Color(100,200,300); //wrong
4. Color color=Color.RED;
5. System.out.println(color); // 调用了toString()方法
6.}
3、所有枚举类都继承了Enum的方法,下面我们详细介绍这些方法。
(1) ordinal()方法: 返回枚举值在枚举类种的顺序。这个顺序根据枚举值声明的顺序而定。
Color.RED.ordinal(); //返回结果:0
Color.BLUE.ordinal(); //返回结果:1
(2) compareTo()方法: Enum实现了java.lang.Comparable接口,因此可以比较象与指定对象的顺序。Enum中的compareTo返回的是两个枚举值的顺序之差。当然,前提是两个枚举值必须属于同一个枚举类,否则会抛出ClassCastException()异常。(具体可见源代码)
Color.RED.compareTo(Color.BLUE); //返回结果 -1
(3) values()方法: 静态方法,返回一个包含全部枚举值的数组。
Color[] colors=Color.values();
for(Color c:colors){
System.out.print(c+",");
}//返回结果:RED,BLUE,BLACK YELLOW,GREEN,
(4) toString()方法: 返回枚举常量的名称。
Color c=Color.RED;
System.out.println(c);//返回结果: RED
(5) valueOf()方法: 这个方法和toString方法是相对应的,返回带指定名称的指定枚举类型的枚举常量。
Color.valueOf("BLUE"); //返回结果: Color.BLUE
(6) equals()方法: 比较两个枚举类对象的引用
lucene的Version定义在org.apache.lucene.util内:
LUCENE_20表示lucene 2.0
LUCENE_35表示lucene 3.5
package org.apache.lucene.util;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Use by certain classes to match version compatibility
* across releases of Lucene.
*
*
WARNING: When changing the version parameter
* that you supply to components in Lucene, do not simply
* change the version at search-time, but instead also adjust
* your indexing code to match, and re-index.
*/
// remove me when java 5 is no longer supported
// this is a workaround for a JDK bug that wrongly emits a warning.
@SuppressWarnings("dep-ann")
public enum Version {
/** Match settings and bugs in Lucene's 2.0 release.
* @deprecated (3.1) Use latest
*/
@Deprecated
LUCENE_20,
/** Match settings and bugs in Lucene's 2.1 release.
* @deprecated (3.1) Use latest
*/
@Deprecated
LUCENE_21,
/** Match settings and bugs in Lucene's 2.2 release.
* @deprecated (3.1) Use latest
*/
@Deprecated
LUCENE_22,
/** Match settings and bugs in Lucene's 2.3 release.
* @deprecated (3.1) Use latest
*/
@Deprecated
LUCENE_23,
/** Match settings and bugs in Lucene's 2.4 release.
* @deprecated (3.1) Use latest
*/
@Deprecated
LUCENE_24,
/** Match settings and bugs in Lucene's 2.9 release.
* @deprecated (3.1) Use latest
*/
@Deprecated
LUCENE_29,
/** Match settings and bugs in Lucene's 3.0 release. */
LUCENE_30,
/** Match settings and bugs in Lucene's 3.1 release. */
LUCENE_31,
/** Match settings and bugs in Lucene's 3.2 release. */
LUCENE_32,
/** Match settings and bugs in Lucene's 3.3 release. */
LUCENE_33,
/** Match settings and bugs in Lucene's 3.4 release. */
LUCENE_34,
/**
* Match settings and bugs in Lucene's 3.5 release.
*
* Use this to get the latest & greatest settings, bug
* fixes, etc, for Lucene.
*/
LUCENE_35,
/* Add new constants for later versions **here** to respect order! */
/**
*
WARNING: if you use this setting, and then
* upgrade to a newer release of Lucene, sizable changes
* may happen. If backwards compatibility is important
* then you should instead explicitly specify an actual
* version.
*
* If you use this constant then you may need to
* re-index all of your documents when upgrading
* Lucene, as the way text is indexed may have changed.
* Additionally, you may need to re-test your entire
* application to ensure it behaves as expected, as
* some defaults may have changed and may break functionality
* in your application.
* @deprecated Use an actual version instead.
*/
@Deprecated
LUCENE_CURRENT;
public boolean onOrAfter(Version other) {
return compareTo(other) >= 0;
}
}
上述代码中,源代码标记@Deprecated是在JDK1.5中作为内置的annotation引入的,用于表明类(class)、方法(method)、字段(field)已经不再推荐使用,并且在以后的JDK版本中可能将其删除,编译器在默认情况下检测到有此标记的时候会提示警告信息。
在后述版本中
@Deprecated
LUCENE_29
2.9及以前版本的可能会被删除