补充几点:
1.枚举对象是可以用 == 比较。
2.
TestEnum3反编译结果:
F:\tree\Test\src\test>javap TestEnum3*
Compiled from "TestEnum3.java"
final class test.TestEnum3$1 extends test.TestEnum3 {
test.TestEnum3$1(java.lang.String, int);
void discription();
}
Compiled from "TestEnum3.java"
final class test.TestEnum3$2 extends test.TestEnum3 {
test.TestEnum3$2(java.lang.String, int);
void discription();
}
Compiled from "TestEnum3.java"
final class test.TestEnum3$3 extends test.TestEnum3 {
test.TestEnum3$3(java.lang.String, int);
void discription();
}
Compiled from "TestEnum3.java"
public abstract class test.TestEnum3 extends java.lang.Enum<test.TestEnum3> {
public static final test.TestEnum3 GREEN;
public static final test.TestEnum3 RED;
public static final test.TestEnum3 YELLOW;
public static test.TestEnum3[] values();
public static test.TestEnum3 valueOf(java.lang.String);
abstract void discription();
public static void main(java.lang.String[]);
test.TestEnum3(java.lang.String, int, test.TestEnum3$1);
static {};
}
TestEnum5反编译结果:
public final class test.TestEnum5 extends java.lang.Enum<test.TestEnum5> {
public static final test.TestEnum5 RED;
public static final test.TestEnum5 GREEN;
public static final test.TestEnum5 YELLOW;
public static test.TestEnum5[] values();
public static test.TestEnum5 valueOf(java.lang.String);
public java.lang.String getColor();
public int getIndex();
public java.lang.String toString();
public static java.lang.String getInstance(int);
public static void main(java.lang.String[]);
static {};
}
package test; enum Colors {
RED, YELLOW, WHITE, GREEN
} /**
*
* @Function:TestEnum
* @Description: 枚举做常量
* @author
* @date :2018/04/10上午11:00:09
*
*/
public class TestEnum { public static void main(String[] args) {
for (Colors string : Colors.values()) {
System.out.println("color:" + string);
}
System.out.println("====================");
for (Colors string : Colors.values()) {
System.out.println(string + " ordinal " + string.ordinal());
}
}
}
/*
* color:RED
* color:YELLOW
* color:WHITE
* color:GREEN
* ====================
* RED ordinal 0
* YELLOW ordinal 1
* WHITE ordinal 2
* GREEN ordinal 3
*/
package test; enum Color {
GREEN, RED, YELLOW, White
} /**
*
* @Function:TestEnum
* @Description: 枚举作用于switch
* @author
* @date :2018/04/10上午11:00:09
*
*/
public class TestEnum1 {
Color color = Color.White; public static void main(String[] args) { TestEnum1 testEnum = new TestEnum1();
System.out.println(testEnum.change()); } public Color change() {
switch (color) { case GREEN:
color = Color.GREEN;
break;
case RED:
color = Color.RED;
break;
case YELLOW:
color = Color.YELLOW;
break;
case White:
color = Color.White;
break;
}
return color;
}
}
/*
* White
*/
package test; /**
*
* @Function:TestEnum
* @Description: 枚举添加方法
* @author
* @date :2018/04/10上午11:00:09
*
*/
public enum TestEnum2 {
RED("红色", 1), YELLOW("黄色", 2), WHITE("白色", 3); private String color;
private int index; // 私有构造方法
private TestEnum2(String color, int index) {
this.color = color;
this.index = index;
} // 通过index获取颜色的静态方法
public static String getColor(int index) {
for (TestEnum2 colorObj : TestEnum2.values()) {
if (colorObj.getIndex() == index) {
return colorObj.getColor();
}
}
return null;
} public String getColor() {
return color;
} public int getIndex() {
return index;
} public static void main(String[] args) {
System.out.println(getColor(2));
}
}
/*
* 黄色
*/
package test; /**
*
* @Function:TestEnum3
* @Description:枚举多态性,添加抽象方法
* @author
* @date :2018/04/10上午11:04:27 编译javac **.java ,反编译 javap **
* 反编译后发现,"枚举常量"继承了TestEnum3重写了抽象方法,此类java.lang.Enum抽象类子类,反编译后此类为抽象类
*/
public enum TestEnum3 {
GREEN {
void discription() {
System.out.println("绿灯行!");
}
},
RED {
void discription() {
System.out.println("红灯停!");
}
},
YELLOW {
void discription() {
System.out.println("黄灯等一等!");
}
};
abstract void discription(); public static void main(String[] args) {
for (TestEnum3 s : TestEnum3.values()) {
s.discription();
}
}
}
/*
* 绿灯行!
* 红灯停!
* 黄灯等一等!
*/
package test; /**
*
* @Function:TestEnum4
* @Description: 利用构造器为实例添加描述
* @author
* @date :2018/04/10下午12:06:54
*
*/
public enum TestEnum4 {
RED("红色"), GREEN("绿色"), YELLOW("黄色"); //实为类对象的实例引用 public String colorFlag; // 我的理解是,枚举常量内的参数(描述)需要跟这个私有的构造器参数一致
private TestEnum4(String flag) {
this.colorFlag = flag;
} public String getColorFlag() {
return colorFlag;
} public static void main(String[] args) {
for (TestEnum4 s : TestEnum4.values()) {
System.out.println(s.getColorFlag());
}
} }
/*
* 红色
* 绿色
* 黄色
*/
package test; /**
*
* @Function:TestEnum5
* @Description: 覆盖枚举的方法
* @author
* @date :2018/04/10下午12:06:54 ================================在本类不能实例化(不知原因)
*/
public enum TestEnum5 {
RED("红色", 1), GREEN("绿色", 2), YELLOW("黄色", 3); private String color;
private int index; // 我的理解是,枚举常量内的参数(描述)需要跟这个私有的构造器参数一致
private TestEnum5(String color, int index) {
this.color = color;
this.index = index;
} public String getColor() {
return color;
} public int getIndex() {
return index;
} @Override
public String toString() {
return this.index + " _color:" + this.color;
} public static String getInstance(int index) {
for (TestEnum5 s : TestEnum5.values()) {
if (s.getIndex() == index) {
// 入果去掉重写toString方法,则返回RED
return s.toString();
}
}
return null;
} public static void main(String[] args) {
System.out.println(getInstance(2));
}
}
/*
* 2 _color:绿色
*/
package test; interface Color6 {
void print(); String getColor();
} /**
*
* @Function:TestEnum6
* @Description: 枚举实现接口
* @author
* @date :2018/04/10下午1:44:46
*
*/
public enum TestEnum6 implements Color6 {
RED("红色", 1), GREEN("绿色", 2), YELLOW("黄色", 3); private String color;
private int index; private TestEnum6(String color, int index) {
this.color = color;
this.index = index;
} @Override
public void print() {
//补充:this.name 可以返回此枚举常量名称
System.out.println(this.index + " color:" + this.color);
} @Override
public String getColor() {
return this.color;
} public static void main(String[] args) {
for (TestEnum6 t : TestEnum6.values()) {
t.print();
}
}
}
/*
* 1 color:红色
* 2 color:绿色
* 3 color:黄色
*/
package test; interface Color7 { enum colors1 implements Color7 {
YELLOW_FRUIT, GREEN_FRUIT, RED_FRUIT
} enum colors2 implements Color7 {
YELLOW, GREEN, RED
}
} /**
*
* @Function:TestEnum6
* @Description: 使用接口组织枚举
* @author
* @date :2018/04/10下午1:44:46
*
*/
public class TestEnum7 implements Color7 {
public static void testImplInstance() {
for (Color7.colors1 c1 : Color7.colors1.values()) {
System.out.println(c1.name());
}
System.out.println("==================");
for (Color7 c2 : Color7.colors2.values()) {
System.out.println(c2);
}
System.out.println("==================");
// 搞个实现接口,来组织枚举,简单讲,就是分类吧。如果大量使用枚举的话,这么干,在写代码的时候,就很方便调用啦。
// 还有就是个“多态”的功能吧,
Color7 color = Color7.colors2.YELLOW;
System.out.println(color);
} public static void main(String[] args) {
testImplInstance();
}
}
/*
* YELLOW_FRUIT
* GREEN_FRUIT
* RED_FRUIT
* ==================
* YELLOW
* GREEN
* RED
* ==================
* YELLOW
*/