补充几点:

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
*/

java枚举使用 总结的更多相关文章

  1. Java 枚举用法详解

    概念 enum 的全称为 enumeration, 是 JDK 1.5 中引入的新特性. 在Java中,被 enum 关键字修饰的类型就是枚举类型.形式如下: enum Color { RED, GR ...

  2. java 枚举类型分析

    最近做android开发,需要用到枚举值,这样可以连续赋值,我按之前c++那样书写,如下所示: public enum ColorSelect { RED_BAGE = 0, GREEN_BAGE, ...

  3. 【转】java枚举类型enum的使用

    原文网址:http://blog.csdn.net/wgw335363240/article/details/6359614 java 枚举类型enum 的使用 最近跟同事讨论问题的时候,突然同事提到 ...

  4. 【转】Java 枚举7常见种用法

    原文网址:http://softbeta.iteye.com/blog/1185573 Java 枚举7常见种用法 博客分类: java java枚举enmu  原创地址:http://blog.li ...

  5. 【转】掌握java枚举类型(enum type)

    原文网址:http://iaiai.iteye.com/blog/1843553 1   背景 在java语言中还没有引入枚举类型之前,表示枚举类型的常用模式是声明一组具有int常量.之前我们通常利用 ...

  6. 转载 java枚举类型enum的使用 (原文地址:http://blog.csdn.net/wgw335363240/article/details/6359614)

    java枚举类型enum的使用 最近跟同事讨论问题的时候,突然同事提到我们为什么java中定义的常量值不采用enmu枚举类型,而采用public final static 类型来定义呢?以前我们都是采 ...

  7. Java枚举类使用

    用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. p ...

  8. Java 枚举类型简介

    目录 Java 枚举示例 Java 枚举构造函数 枚举类型是用于定义常量集合的特殊类型,更确切的说,JAVA枚举类型是一种特殊的 java 类.枚举类型可以包含常量.方法等.在 java5 中添加了 ...

  9. Java 枚举(enum) 详解7种常见的用法

    Java 枚举(enum) 详解7种常见的用法 来源 https://blog.csdn.net/qq_27093465/article/details/52180865 JDK1.5引入了新的类型— ...

  10. 使用javap进行反编译Java枚举

    这是一个枚举类Day.java public enum Day { MONDAY("星期一"), TUESDAY("星期二"), WEDNESDAY(" ...

随机推荐

  1. 【medium】990. Satisfiability of Equality Equations 并查集

    Given an array equations of strings that represent relationships between variables, each string equa ...

  2. 【interview】Microsoft面经

    ~~收集的面经~~ 1. 实现hashtable的put 和get操作 参考:https://yikun.github.io/2015/04/01/Java-HashMap%E5%B7%A5%E4%B ...

  3. pwnable.tw start&orw

    emm,之前一直想做tw的pwnable苦于没有小飞机(,今天做了一下发现都是比较硬核的pwn题目,对于我这种刚入门?的菜鸡来说可能难度刚好(orz 1.start 比较简单的一个栈溢出,给出一个li ...

  4. 不能忽视 php warning

    2018-12-5 10:50:22 星期三 遇到一个问题: 前一行是取数组中的某一个值, 后一行是用heredoc输出一串javascript脚本; 因为取数组中值的时候键不存在, 导致后续输出的j ...

  5. python正则表达式--findall、finditer方法

    findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...

  6. curl常用命令备忘

    #####(输出请求头信息) curl -I xxx-Pro:test xxx$ curl -I https://www.baidu.com/ HTTP/1.1 200 OK Accept-Range ...

  7. 抓取某网站信息时遇到的问题及解决 The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set

    var response = httpClient.SendAsync(requestMessage).Result; content = response.Content.ReadAsStringA ...

  8. requireJS简单应用

    项目结构目录: Html页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...

  9. Docker使用Mysql镜像命令

    本次使用的环境是win10下的hyper-v安装的CentOS7系统 控制台输入命令: docker run -p 3307:3306 --name mysql01 -v $PWD/conf:/etc ...

  10. 028 kafka面试小节

    1.大纲 Kafka控制节点用的是什么? 消费者.生产者是如何理解的? 2.Kafka控制节点用的是什么? 基于zookeeper协调的分布式消息系统 3.消费者.生产者是如何理解的? 消息系统通常都 ...