重写enum的valueof方法等
enum 对象的常用方法介绍
int compareTo(E o) 
          比较此枚举与指定对象的顺序。
Class<E> getDeclaringClass() 
          返回与此枚举常量的枚举类型相对应的 Class 对象。
String name() 
          返回此枚举常量的名称,在其枚举声明中对其进行声明。
int ordinal() 
          返回枚举常量的序数(它在枚举声明中的位置,其中初始常量序数为零)。
String toString()
           返回枚举常量的名称,它包含在声明中。
static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) 
          返回带指定名称的指定枚举类型的枚举常量。
package test;
public class EnumTest {
    public enum Color {
        RED(, , ), BLUE(, , ), BLACK(, , ), YELLOW(, , ), GREEN(, , );
                                                         // 构造枚举值,比如RED(255,0,0)
        private Color(int rv, int gv, int bv) {
            this.redValue = rv;
            this.greenValue = gv;
            this.blueValue = bv;
        }
        private int redValue;                             // 自定义数据域,private为了封装。
        private int greenValue;
        private int blueValue;
        public static final Color[] values=Color.values();
        public static Color valueOf(int i) {
            return values[i];
        }
        public String toString() {                         // 覆盖了父类Enum的toString()
            return super.toString() + "(" + redValue + "," + greenValue + "," + blueValue + ")";
        }
    }
    public enum ColorType{
        Red(Color.RED),
        Blue(Color.BLUE),
        Black(Color.BLACK),
        Yellow(Color.YELLOW),
        Green(Color.GREEN);
        private Color colorId;
        private ColorType(Color colorId) {
            this.colorId=colorId;
        }
        public static ColorType[] a=ColorType.values();
        public static ColorType valueOf(int i) {
            return a[i];
        }
        public String toString() {
            return super.toString()+"-------------->"+colorId.toString();
        }
    }
    public static void main(String args[]) {
        // Color colors=new Color(100,200,300); //wrong
        Color color = Color.RED;
        Color colorYellow=Color.YELLOW;
        System.out.println(color);                         // 调用了toString()方法
        System.out.println(color.ordinal());
        System.out.println(color.compareTo(colorYellow));  //返回的是两个枚举值的顺序之差
        System.out.println(Color.valueOf("BLUE"));
        System.out.println(Color.valueOf());              //重写valueOf方法  
        System.out.println(ColorType.valueOf().toString());
    }
}
运行结果:
RED(,,) -
BLUE(,,)
BLUE(,,)
Black-------------->BLACK(,,)
自定义方法:
package test;
public class EnumTest3 {
    public enum EnumTest {
        MON(), TUE(), WED(), THU(), FRI(), SAT() {
            @Override
            public boolean isRest() {
                return true;
            }
        },
        SUN() {
            @Override
            public boolean isRest() {
                return true;
            }
        };
        private int value;
        private EnumTest(int value) {
            this.value = value;
        }
        public int getValue() {
            return value;
        }
        public boolean isRest() {
            return false;
        }
    }
    public static void main(String[] args) {
        System.out.println("EnumTest.FRI 的 value = " + EnumTest.SAT.isRest());
    }
}
输出结果:
EnumTest.FRI 的 value = true
重写enum的valueof方法等的更多相关文章
- 枚举类valueOf方法的疑问
		枚举类中valueOf方法只有一个参数而Enum类中有两个参数,请问Enum实例类中的valueOf方法是从何处继承而来? 答案:jvm进行编译的时候添加的. 
- JavaScript系列-----Object之toString()和valueOf()方法 (2)
		深入理解toString()和valueOf()函数 1.我们为什么要了解这两种方法 众所周知,toString()函数和valueOf函数,这两个函数是Object类的对象生来就拥有的,而且他们还可 ... 
- 重写jquery的ajax方法
		//首先备份下jquery的ajax方法 var _ajax=$.ajax; //重写jquery的ajax方法 $.ajax=function(opt){ //备份opt中error和success ... 
- 子类可以有跟父类中同名的方法,但是会重写父类中的方法,甚至是root class中的方法
		/* 子类可以重写父类中的方法,甚至是root class中的方法,比如NSObeject 的new方法,但是后提示警告如下 Method is expected to return an insta ... 
- js中toString和valueOf方法的区别
		toString 方法 返回对象的字符串表示形式. 语法:objectname.toString([radix]) objectname 必需.要为其搜索字符串表示形式的对象. radix 可选.为将 ... 
- JavaScript的toString()和valueof()方法
		toString()方法: 函数:函数 (function(){}).toString(); //返回"function(){}" typeof((function(){}).to ... 
- valueOf()方法
		前面的话 关于类型转换,对象常见的两个方法是toString()和valueOf().实际上,这两个方法也可以应用在包装类型上.前面已经介绍过toString()方法,本文将介绍valueOf()方法 ... 
- 如何真正重写window对象的方法
		重写window对象的方法不是一件新奇的事,比如我们可能需要改变默认alert的行为,如何安全的重写呢? 小菜看到某知名IT网站是这样的写法: window.alert = function(){}; ... 
- Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例(转)
		Java中==.equals.hashcode的区别与重写equals以及hashcode方法实例 原文地址:http://www.cnblogs.com/luankun0214/p/4421770 ... 
随机推荐
- Centos7 安装python3.5.3
			使用root用户安装:切换到root用户 su 回车,然后输入密码,切换到root用户. 新建一个脚本,如installPython.sh #!/bin/bash yum -y install zli ... 
- 使用gcc -g编译,gdb调试时仍然存在“no debug symbols found”的错误
			今天为调试一段代码,使用gcc将程序用-g选项重新编译.但是使用gdb进行debug时,仍然出现“no debug symbols found”的错误.仔细检查了一下Makefile,原来后面定义的连 ... 
- jdk环境变量配置(参考自《疯狂java讲义》)
			做个记录,免得每次配环境都要百度 环境变量的配置 path环境变量配置的作用:程序的执行需要使用外部指令javac, 但是javac指令仅仅能在JDK安装目录下的bin目录下使用,因此程序只能写入bi ... 
- bzoj5469 [FJOI2018]领导集团问题
			题目描述: bz luogu 题解: 相当于树上$LIS$问题. 考虑一维情况下的贪心,我们可以用multiset启发式合并搞. 代码: #include<set> #include< ... 
- Python自动化测试框架——数据驱动(从文件中读取)
			学过编程的伙伴们都知道,数据不仅可以从代码中读取,还可以从文件中读取. 今天小编就简要的介绍一下从文件中读取数据,并应用到自动化测试中方法. 先来展示下接下来将要用到的文件在项目中的结构 从txt文件 ... 
- MySQL 之视图、 触发器、事务、存储过程、内置函数、流程控制、索引
			本文内容: 视图 触发器 事务 存储过程 内置函数 流程控制 索引 ------------------------------------------------------------------ ... 
- H.264 Profile-level-id
			基于SIP协议的VOIP通信,该字段通常位于视频协商sdp报文中,如: video RTP/AVP rtpmap: H264/ fmtp: profile-level-id=42801E; packe ... 
- linux 环境下bash脚本中找不到命令
			mr.sh: line 1: HADOOP_CMD: command not found mr.sh: line 4: INPUT_FILE_PATH: command not found mr.sh ... 
- 00048_this关键字
			1.this调用构造方法 (1)构造方法之间的调用,可以通过this关键字来完成: (2)构造方法调用格式 this(参数列表); (3)小案例 class Person { // Person的成员 ... 
- MFC CString 和int相互转化
			CString str = _T("123"); int i = _ttoi(str); =============== int i = 123; CString str ; st ... 
