补充几点:

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. noip2018游(AFO)记

    Day 0 到学车了,已经差不多四点了,领完一小袋比赛要用的就匆匆回了宾馆. 话说之前看地图的时候我们的宾馆最远,而且名字听起来并没有怎么高大上, 一看隔壁度豪大酒店就感觉应该比我们的酒店好.然鹅到了 ...

  2. 关于VC预定义常量_WIN32,WIN32,_WIN64等预定义宏的介绍(整理、转载)

    参考帖子: (1)MSDN上专门讲预定义宏:https://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx (2)VS中属性页的配置介绍 ...

  3. 【easy】532. K-diff Pairs in an Array

    这道题给了我们一个含有重复数字的无序数组,还有一个整数k,让我们找出有多少对不重复的数对(i, j)使得i和j的差刚好为k.由于k有可能为0,而只有含有至少两个相同的数字才能形成数对,那么就是说我们需 ...

  4. vue 双向数据绑定的实现学习(一)

    前言:本系列学习笔记从以下几个点展开 什么是双向数据绑定 双向数据绑定的好处 怎么实现双向数据绑定 实现双向数据数据绑定需要哪些知识点 数据劫持 发布订阅模式 先看看我们要实现的目标是什么,如下动图: ...

  5. requests基本应用

    requests基本功能详解 import requests response = requests.get('https://www.baidu.com') print('type属性:',type ...

  6. 微信小程序上的map组件bindregionchange地图视野变化函数成功回调会产生2次值的问题?

    bindregionchange确实是会触发两次,第一次是视野变化开始,第二次是视野变化结束. 你可以尝试把e.type给打印出来,值为begin表示开始,值为end表示结束. wxml: js:

  7. sql查找某一列中某一数值出现次数大于3的记录的前3条

    SELECT * FROM table  GROUP BY column HAVING COUNT(column)>=3 ORDER BY column DESC LIMIT 0,3;

  8. RPA 介绍

    一 术语表 机器人流程自动化(RPA):在数字系统中模拟和集成人类行为以优化业务流程的软件机器人.RPA自动化捕获数据.运行应用程序.触发响应并与其他系统通信以执行各种任务. RPA路线图(RPA r ...

  9. solution for python can not import local module

    blog 这次遇到的问题是sys.path的输出不包含'',导致无法import当前文件和文件夹 When no ._pth file is found, this is how sys.path i ...

  10. Core在类中注入

    private readonly IHttpClientFactory _iHttpClientFactory; public static NetHelper Get = new NetHelper ...