---
title: "java中的枚举使用方法"
author: "chakey"
date: "2011-04-19T13:53:28Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2011-04-20T08:50:10Z"
id: 1007929
url: "https://datalife.iteye.com/blog/1007929"
---

```
public enum FtpActionType {
    LOGIN(1) {
        public String getTitle() {
            return "登陆";
        }
    },
    LOGOUT(2) {
        public String getTitle() {
            return "登出";
        }
    },
    UNKNOWN(99) {
        public String getTitle() {
            return "未知";
        }
    };

    private int code; 

    FtpActionType(int code) { 
        this.code = code;
    }

    @Override
    public String toString() {
        return String.valueOf(code);
    }

    public abstract String getTitle();
}
```

```

```

```
public enum Difficulty {
	// 注意：枚举成员命名，请使用英文大写形式
	NORMAL(1), MEDIUM(2), HARD(3); 
	/**
	 * final修饰字段是一个良好的实践。
	 */
	final private int id;
	
	Difficulty(final int id){
		this.id=id;
	}
	/**
	 * 获得ID
	 * @return
	 */
	public int getId() {
		return id;
	}
}
```

```

```

```
/**

 * 游戏中的难度枚举：NORMAL , MEDIUM, HARD

 * 

 * @author mercyblitz

 */

public enum Difficulty {

NORMAL, MEDIUM, HARD //注意：枚举成员命名，请使用英文大写形式

}
```