`

java 关于枚举类型 valueOf方法

阅读更多

  在java中使用枚举类型时

public enum TestEnum{

TEST1("test1"),

TEST2("test2"),

TEST3("test3"),

TEST4("test4"),;

 

private TestEnum(String code) {

this.code = code;

}

 

private String code;

 

public String getText() {

return this.getText();

}

 

public String getCode() {

return this.code;

}

 

}

 

当想要通过一个String获取枚举时,默认可以使用枚举本身提供的 ValueOf,但是是需要两个参数,

特殊的地方是,String类型的valueOf(String) 方法 ,枚举给隐士提供一个静态方法。

其他类型就需要自己定义喽。

 

TestEnum.valueOf("TEST1");形式获取

 

以下是API描述。

 

 

public static <T extends Enum<T>> T valueOf(Class<T> enumType,
                            String name)
Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Note that for a particular enum type T, the implicitly declared public static T valueOf(String) method on that enum may be used instead of this method to map from a name to the corresponding enum constant. All the constants of an enum type can be obtained by calling the implicit public static T[] values() method of that type.

Type Parameters:
T - The enum type whose constant is to be returned
Parameters:
enumType - the Class object of the enum type from which to return a constant
name - the name of the constant to return
Returns:
the enum constant of the specified enum type with the specified name
Throws:
IllegalArgumentException - if the specified enum type has no constant with the specified name, or the specified class object does not represent an enum type
NullPointerException - if enumType or name is null
Since:
1.5
分享到:
评论

相关推荐

    Java中怎样由枚举常量的ordinal值获得枚举常量对象

    Java1.5提供了关键字enum,能够通过该关键字方便得定义自己须要的枚举类型,比方  enum Season { ...也是说,使用name()方法和valueOf(String)方法能够在枚举类型对象和字符串之间方便得转换。当然,假设valu

    Java 高级特性.doc

    因为程序里面有个静态方法,如果导入import static java.lang.Integer.parseInt;这样的程序就可以运行了。 2.可变参数与for循环增强 这个是一般的用法: public static void loop(String[] args){ for(int i=0;i;i++...

    java 面试题 总结

    Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。Int是java的原始数据类型,Integer是java为int提供的封装类。Java为每个原始类型提供了封装类。 原始类型封装类 booleanBoolean charCharacter byte...

    java逻辑思维笔试题-effective-java-3rd-edition:有效的Java第3版注释

    java逻辑思维笔试题Effective Java - 第三版笔记 章节索引 ...枚举类型提供了这种保证 (PRO) 静态方法可以返回其返回类型的任何子类型的对象,与构造函数不同 这会导致 API 紧凑 适合基于接口的框架(条目

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

     Java 提供两种不同的类型:引用类型和原始类型(或内置类型)。Int是java的原始数据类型,Integer是java为int提供的封装类。Java为每个原始类型提供了封装类。 原始类型 封装类 boolean Boolean char Character ...

    微软JavaScript手册

    indexOf 方法 返回在 String 对象中第一次出现子字符串的字符位置。 不相等运算符 (!=) 比较两个表达式,看是否不相等。 Infinity 属性 返回 Number.POSITIVE_INFINITY 的初始值。 input 属性 返回进行查找的...

    【05-面向对象(下)】

    •如果希望获得包装类对象中包装的基本类型变量,则可以使用包装类提供的XxxValue()实例方法。 自动装箱与自动拆箱 •JDk还提供了自动装箱和自动拆箱。自动装箱就是把一个基本类型的变量直接赋给对应的...

    在一小时内学会 C#(txt版本)

    枚举类型 引用类型 引用类型在堆(heap)中分配内存且当其不再使用时,将自动进行垃圾清理。和 C++ 要求用户显示创建 delete 运算符不一样,它们使用新运算符创建,且没有 delete 运算符。在 C# 中它们自动由...

Global site tag (gtag.js) - Google Analytics