---
title: "枚举中valueOf用法"
author: "latty"
date: "2018-01-14T11:21:06Z"
tags: []
categories: ["编程语言"]
slug: "valueof"
draft: false
lastmod: "2018-01-14T11:21:06Z"
id: 2407692
url: "https://latty.iteye.com/blog/2407692"
---

Enum的特征如下：
1.它不能有public的构造函数，这样做可以保证客户代码没有办法新建一个enum的实例。   
2.所有枚举值都是public   ,   static   ,   final的。注意这一点只是针对于枚举值，
我们可以和在普通类里面定义   变量一样定义其它任何类型的非枚举变量，这些变量可以用任何你想用的修饰符。   

　　3．Enum默认实现了java.lang.Comparable接口。   

　　4．Enum覆载了了toString方法，因此我们如果调用Color.Blue.toString()默认返回字符串”Blue”.   

　 　5．Enum提供了一个valueOf方法，这个方法和toString方法是相对应的。调用valueOf(“Blue”)将返回 Color.Blue.因此我们在自己重写toString方法的时候就要注意到这一点，一把来说应该相对应地重写valueOf方法。   

　　6．Enum还提供了values方法，这个方法使你能够方便的遍历所有的枚举值。   

　　7．Enum还有一个oridinal的方法，这个方法返回枚举值在枚举类种的顺序，这个顺序根据枚举值声明的顺序而定，这里Color.Red.ordinal()返回0。   




```java
public enum Color {
	Red,Green,Blue;
}

	@Test
	public void test1()
	{
		
		System.out.println( Color.values().length);
		System.out.println( Color.Blue.toString());
		System.out.println( Color.valueOf("Blue") == Color.Blue);
	}
	
    执行结果如下：
3
Blue
true
```




下面看有构造的枚举：

```java
public enum ColorConstruct {

	RED(1), GREEN(2), BLANK(3), YELLO(4);
	private int index;
	 
	private ColorConstruct(int index) {
        this.index = index;
    }
}

@Test
	public void test2()
	{
		
		System.out.println( ColorConstruct.values().length);
		System.out.println( ColorConstruct.RED.toString());
		System.out.println( ColorConstruct.valueOf("RED") == ColorConstruct.RED);
	}
执行结果为：
4
RED
true

```