好处:

1.可以直接使用switch

2.可以实现toString()方法

笔记:

1.枚举类头部定义的成员变量,可以看做是枚举类的一个实例

public enum Color {
RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4); // 成员变量 private String name;
private int index;
// 构造方法
private Color(String name, int index) { this.name = name; this.index = index; }
// 普通方法
public static String getName(int index) { for (Color c : Color.values()) { if (c.getIndex() == index) { return c.name; } }
return null; }
// get set 方法
public String getName() { return name; }
}

Enum枚举类型的使用笔记的更多相关文章

  1. Python中模拟enum枚举类型的5种方法分享

    这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下   以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...

  2. enum枚举类型的定义

    enum枚举类型的定义方式与某种用法 #include <iostream> using namespace std; int main() { enum TOT{ zero, one, ...

  3. [小问题笔记(四)] Enum枚举类型转换为DataTable( C# )

    枚举: public enum ProductType { 小产品=, 大产品, 超大产品 } 转换方法: /// <summary> /// 枚举类型转化为DataTable /// & ...

  4. enum枚举类型的使用

    修饰符为public static enum,不用加final,否则提示错误. 枚举类的所有实例必须在枚举类中显式列出(,分隔,; 结尾).列出的实例系统会自动添加 public static fin ...

  5. C语言--enum,typedef enum 枚举类型详解

    原文:http://z515256164.blog.163.com/blog/static/32443029201192182854300/ 有改动 C语言详解 - 枚举类型 注:以下全部代码的执行环 ...

  6. Java中的Enum枚举类型总结

    废话不多说,直接上代码,该例子来源于:http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html public enum Planet { ...

  7. C# Enum枚举类型操作扩展类

    使用示例: using System.ComponentModel; namespace SchoolEnterpriseManageSys.Enum { /// <summary> // ...

  8. Java enum枚举类型

    java的枚举类型详解: 简单示例: public enum Color{ RED,BLUE,BLACK,YELLOW,GREEN } 复杂示例(带自定义构造方法与类型) public enum En ...

  9. C++ enum 枚举类型

    1. 枚举类型浅谈 假设我们要设计一个打开文件的函数, 打开文件由三种状态: input, output 和 append. 不使用枚举, 我们可能会写出如下的代码 const int input = ...

随机推荐

  1. POJ 3308 Paratroopers(最小割EK(邻接表&矩阵))

    Description It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the ...

  2. 产生library cache latch原因

    产生library cache latch原因The library cache latches protect the cached SQL statements and objects' defi ...

  3. cocos2d3.0跑酷代码讲解和源码

    最近在网上看到一个跑酷代码的例子,写的很不错,连接如下 http://www.waitingfy.com/archives/783 现在我把他精简了一下,去除了一些比较简单的特效,着重保留了主角的跳跃 ...

  4. c/c++笔记

    string 若要根据字典序比较string类型的大小,只需要用><=就可以啦 例如: string s1="abcz"; string s2="abcd&q ...

  5. python学习笔记(sqlalchemy安装失败)

    博主今天折腾了一天sqlalchemy安装问题 下面是失败的截图 直接重新安装python2.7仍未解决此问题 最终在一国外论坛中找到解决的方法: 安装VC++对python的支持文件 VCForPy ...

  6. 有关OpenCV1.0中GUI命令的几个函数学习总结

    1.修改窗口背景色或者光标形状 在OpenCV1.0版本利用函数int cvNamedWindow( const char* name, int flags )初始化创建一个窗口后,窗口的背景色是灰色 ...

  7. Class attributes

    In order to print Card objects in a way that people can easily read, we need a mapping from the inte ...

  8. 一目了然c++虚基类!

    #include <IOSTREAM.H> //基类 class CBase ...{ protected: int a; public: CBase(int na) ...{ a=na; ...

  9. iOS地址编码解析

    - (void)viewDidLoad { [super viewDidLoad]; // 创建地址解析器 self.geocoder = [[CLGeocoder alloc] init]; } - ...

  10. hql语句关联查询(select new )

    在  new bean()中,对象里要有其构造方法,查询参数必须是构造方法中的参数,get set也要同步 类似于 String hql= "select  new A(a.id, a.na ...