`

枚举中valueOf用法

阅读更多
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。  



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




下面看有构造的枚举:
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















分享到:
评论

相关推荐

    Enum.lua:Lua可用的枚举

    -- To control the starting value of an enum, the first value can be set. local States = Enum ({ [ " Paused " ] = - 1 , -- Our enum will now start at -1 instead of 0. " Active " , -- > 0 " Win " , -...

    all-keys:获取对象的所有属性键,包括不可枚举和继承的

    'valueOf' ] */ allKeys ( Symbol . prototype ) ; /* Set { 'constructor', 'toString', 'valueOf', 'toLocaleString', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', '__defineGetter__',...

    超级有影响力霸气的Java面试题大全文档

    引用类型和原始类型具有不同的特征和用法,它们包括:大小和速度问题,这种类型以哪种类型的数据结构存储,当引用类型和原始类型用作某个类的实例数据时所指定的缺省值。对象引用实例变量的缺省值为 null,而原始...

    微软C#语言规范,C#语言教程中文版

    4.1.1 System.ValueType 类型 78 4.1.2 默认构造函数 78 4.1.3 结构类型 79 4.1.4 简单类型 79 4.1.5 整型 80 4.1.6 浮点型 81 4.1.7 decimal 类型 82 4.1.8 bool 类型 82 4.1.9 枚举类型 82 4.1.10 可以为 null 的...

    C#语言规范(4.0版本)

    4.1.1 System.ValueType 类型 78 4.1.2 默认构造函数 78 4.1.3 结构类型 79 4.1.4 简单类型 79 4.1.5 整型 80 4.1.6 浮点型 81 4.1.7 decimal 类型 82 4.1.8 bool 类型 82 4.1.9 枚举类型 82 4.1.10 可以为 null 的...

    [原创]自己工作中常用的模板库,简化你的工作

    这上传的资源中包含一套我工作中常用的模板库,及不需要MFC支持的excel操作接口,导出函数调用栈(dump stack)接口,可以直接用VS2008运行TestCodeLib.sln来根据unit test来了解用法。 ⑴ 需求(requirements) 重量级...

    C#语言规范4.0

    4.1.1 System.ValueType 类型 78 4.1.2 默认构造函数 78 4.1.3 结构类型 79 4.1.4 简单类型 79 4.1.5 整型 80 4.1.6 浮点型 81 4.1.7 decimal 类型 82 4.1.8 bool 类型 82 4.1.9 枚举类型 82 4.1.10 可以为 null 的...

    C#_语言规范_4.0_中文版

    4.1.1 System.ValueType 类型 78 4.1.2 默认构造函数 78 4.1.3 结构类型 79 4.1.4 简单类型 79 4.1.5 整型 80 4.1.6 浮点型 81 4.1.7 decimal 类型 82 4.1.8 bool 类型 82 4.1.9 枚举类型 82 4.1.10 可以为 null 的...

    C#语言规范(2.0,3.0,4.0合集)

    4.1.1 System.ValueType 类型 78 4.1.2 默认构造函数 78 4.1.3 结构类型 79 4.1.4 简单类型 79 4.1.5 整型 80 4.1.6 浮点型 81 4.1.7 decimal 类型 82 4.1.8 bool 类型 82 4.1.9 枚举类型 82 4.1.10 可以为 null 的...

    c++基础教程,简洁扼要

    因为该函数库在C++中仍然有效,并且在一些现存的程序仍然使用,所以我们下面将学习一些关于这个函数库中的函数用法。 函数malloc void * malloc (size_t nbytes); char * cp; cp = (char *) malloc (10); int * ...

    C#教程(语言规范)

    4.1.1 System.ValueType 类型 ... 72 4.1.2 默认构造函数... 72 4.1.3 结构类型... 73 4.1.4 简单类型... 73 4.1.5 整型... 74 4.1.6 浮点型. 75 4.1.7 decimal 类型 ... 76 4.1.8 bool 类型... 76 4.1.9 ...

    如何编写批处理文件批处理文件批处理文件

    会枚举当前环境中的环境变量名称。 另外,FOR 变量参照的替换已被增强。您现在可以使用下列 选项语法: ~I - 删除任何引号("),扩充 %I %~fI - 将 %I 扩充到一个完全合格的路径名 %~dI - 仅将 %I 扩充到一个驱动...

    java 面试题 总结

    引用类型和原始类型具有不同的特征和用法,它们包括:大小和速度问题,这种类型以哪种类型的数据结构存储,当引用类型和原始类型用作某个类的实例数据时所指定的缺省值。对象引用实例变量的缺省值为 null,而原始...

    语言程序设计课后习题答案

    2-6 在下面的枚举类型中,Blue的值是多少? enum COLOR { WHITE, BLACK = 100, RED, BLUE, GREEN = 300 }; 解: Blue = 102 2-7 注释有什么作用?C++中有哪几种注释的方法?他们之间有什么区别? 解: 注释在...

    grub4dos-V0.4.6a-2017-02-04更新

    3.two variables boot_drive and install_partition can be preset to a value before transferring control to grub4dos. (tinybit) 4.修正屏蔽 map 信息。(yaya) 2014-10-09(yaya) 1.屏蔽 map 信息。 2014...

    Java 高级特性.doc

    public String value() default "yellow"; public int[] array() default {1,2}; public MetaAnnotation annotation() ; } 注解最主要的就是这么多,其实注解真正的,我们都很少留心的,但是有的注解会给你在写程序...

Global site tag (gtag.js) - Google Analytics