java.lang.Enum对枚举的实现和具体用法

java.lang.Enum类:对枚举的定义:

public abstract class Enum>
extends Object
implements Comparable, Serializable;// 抽象枚举类
This is the common base class of all Java language enumeration types.
// 构造方法:
protected         Enum(String name, int ordinal)
          Sole constructor.
//Method Summary
protected  Object         clone()
          Throws CloneNotSupportedException.
int         compareTo(E o)
          Compares this enum with the specified object for order.
boolean         equals(Object other)
          Returns true if the specified object is equal to this enum constant.
protected  void         finalize()
          enum classes cannot have finalize methods.
Class         getDeclaringClass()
          Returns the Class object corresponding to this enum constant’s enum type.
int         hashCode()
          Returns a hash code for this enum constant.
String         name()        // 获取枚举常量名
          Returns the name of this enum constant, exactly as declared in its enum declaration.
int         ordinal()        // 获取枚举常量序号
          Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).
String         toString()
          Returns the name of this enum constant, as contained in the declaration.
static > T valueOf(Class enumType, String name)
          Returns the enum constant of the specified enum type with the specified name.
//enum关键字使用后,被声明的类型默认继承Enum类,可以使用Enum的所有接口;
//而且可以有多种方式实现枚举常量的定义:
—–1.使用默认方式定义枚举常量:
package com.mldn;
/*
        jdk1.5后,引人了枚举类型:关键字:enum: [public] enum enumName{….,枚举常量,…}
枚举类型:可以同时具有多种身份,但同一场合只使用一种身份!
*/
enum Color        // enum关键字 使Color继承Enum类
{
        RED, BLUE, GREEN;        // 没有构造方法,将默认调用Enum的构造方法
}
public class EnumDemo2
{
        public static void main(String[] args)
        {
                for (Color c : Color.values())
                {
                        System.out.println(c.ordinal() + ” –> ” + c.name());        // 使用继承的方法输出序号、枚举名字
                }
        }       
}
/*
ubuntu@xu-desktop:~$ java com.mldn.EnumDemo2
0 –> RED        // 这里的序号
1 –> BLUE
2 –> GREEN
*/
——–2.使用自定义方式:
package com.mldn;
enum Color
{
        RED(“红色”), BLUE(“蓝色”), GREEN(“绿色”);        // 枚举常量调用本地有参构造
        private String name = null;        // name属性
        private Color(String name)        //
        {
                this.setName(name);        // 枚举类型规定单例设计
        }       
        public String getName()
        {
                return this.name;
        }
        public void setName(String name)
        {
                this.name = name;
        }
}
public class EnumDemo3
{
        public static void main(String[] args)
        {
                for (Color c : Color.values())
                {
                        System.out.println(c.ordinal() + ” –> ” + c.name() + “–>” + c.getName());        // 使用继承的方法输出序号、枚举名字
                }
        }       
}
/*
ubuntu@xu-desktop:~$ java com.mldn.EnumDemo3
0 –> RED–>红色
1 –> BLUE–>蓝色
2 –> GREEN–>绿色
*/
———–3.不使用自定义构造:
package com.mldn;
enum Color
{
        RED, BLUE, GREEN;        // 枚举常量
        private String name = null;        // name属性
        public String getName()
        {
                return this.name;
        }
        public void setName(String name) // 设置name属性
        {
                switch (this)
                {
                        case RED:
                        {
                                if (“红色”.equals(name))
                                {
                                        this.name = name;        // 匹配则修改
                                }
                                else
                                {
                                        System.out.println(“设置错误!”);
                                }
                                break;        // 退出循环
                        }
                        case BLUE:
                        {
                                if (“蓝色”.equals(name))
                                {
                                        this.name = name;        // 匹配则修改
                                }
                                else
                                {
                                        System.out.println(“设置错误!”);
                                }
                                break;        // 退出循环
                        }
                        case GREEN:
                        {
                                if (“绿色”.equals(name))
                                {
                                        this.name = name;        // 匹配则修改
                                }
                                else
                                {
                                        System.out.println(“设置错误!”);
                                }
                                break;        // 退出循环
                        }
                }
                // this.name = name;
        }
}
public class EnumDemo3
{
        public static void main(String[] args)
        {
                Color c = Color.BLUE;        // 获取颜色
                c.setName(“蓝色”);       
                c.setName(“兰色”);                                       
           System.out.println(c.ordinal() + ” –> ” + c.name() + ” –> ” + c.getName());        // 使用继承的方法输出序号、枚举名字
        }       
}
/*
ubuntu@xu-desktop:~$ java com.mldn.EnumDemo3
设置错误!
1 –> BLUE –> 蓝色
*/
———–返回enum常量:
public static > T valueOf(Class 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.)
package com.mldn;
enum Color
{
        RED, BLUE, GREEN;        // 枚举常量
        private String name = null;        // name属性
        public String getName()
        {
                return this.name;
        }
        public void setName(String name) // 设置name属性
        {
                switch (this)
                {
                        case RED:
                        {
                                if (“红色”.equals(name))
                                {
                                        this.name = name;        // 匹配则修改
                                }
                                else
                                {
                                        System.out.println(“设置错误!”);
                                }
                                break;        // 退出循环
                        }
                        case BLUE:
                        {
                                if (“蓝色”.equals(name))
                                {
                                        this.name = name;        // 匹配则修改
                                }
                                else
                                {
                                        System.out.println(“设置错误!”);
                                }
                                break;        // 退出循环
                        }
                        case GREEN:
                        {
                                if (“绿色”.equals(name))
                                {
                                        this.name = name;        // 匹配则修改
                                }
                                else
                                {
                                        System.out.println(“设置错误!”);
                                }
                                break;        // 退出循环
                        }
                }
                // this.name = name;
        }
}
public class EnumDemo3
{
        public static void main(String[] args)
        {
                Color c = Color.valueOf(Color.class, “BLUE”);        // 获取枚举常量,使用反射机制
                c.setName(“蓝色”);       
                c.setName(“兰色”);
                System.out.println(c.ordinal() + ” –> ” + c.name() + ” –> ” + c.getName());        // 使用继承的方法输出序号、枚举名字
        }       
}
/*
ubuntu@xu-desktop:~$ java com.mldn.EnumDemo3
设置错误!
1 –> BLUE –> 蓝色
*/   
———-按枚举常量序号输出排序集合:
package com.mldn;
import java.util.Set;
import java.util.TreeSet;
import java.util.Iterator;
enum Color
{
        RED, BLUE, GREEN;        // 枚举常量
        private String name = null;        // name属性
        public String getName()
        {
                return this.name;
        }
        public void setName(String name) // 设置name属性
        {
                switch (this)
                {
                        case RED:
                        {
                                if (“红色”.equals(name))
                                {
                                        this.name = name;        // 匹配则修改
                                }
                                else
                                {
                                        System.out.println(“设置错误!”);
                                }
                                break;        // 退出循环
                        }
                        case BLUE:
                        {
                                if (“蓝色”.equals(name))
                                {
                                        this.name = name;        // 匹配则修改
                                }
                                else
                                {
                                        System.out.println(“设置错误!”);
                                }
                                break;        // 退出循环
                        }
                        case GREEN:
                        {
                                if (“绿色”.equals(name))
                                {
                                        this.name = name;        // 匹配则修改
                                }
                                else
                                {
                                        System.out.println(“设置错误!”);
                                }
                                break;        // 退出循环
                        }
                }
                // this.name = name;
        }
}
public class EnumDemo3
{
        public static void main(String[] args)
        {
                Set c = new TreeSet();        // 实例化
                c.add(Color.BLUE);                // 添加元素
                c.add(Color.RED);
                c.add(Color.GREEN);
                Iterator        iter = c.iterator();       
                System.out.println(“排序的枚举常量:”);
                while (iter.hasNext())
                {
                        System.out.print(iter.next() + ” “);
                }
                System.out.println();
        }       
}
/*
ubuntu@xu-desktop:~$ java com.mldn.EnumDemo3
排序的枚举常量:
RED BLUE GREEN // Enum类型实现了Comparable接口,支持按常量序号排序!
*/

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《java.lang.Enum对枚举的实现和具体用法
本文地址:http://www.xiupu.net/archives-132.html
关注公众号:嗅谱网

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!