Java枚举以及实现原理

不要让这个世界的复杂性阻碍你的前进。 要成为一个行动主义者,将解决人类的不平等视为己任。 它将成为你生命中最重要的经历之一。
——比尔·盖茨在哈佛大学的演讲

声明枚举

[public]enum 枚举类型名称[implements 接口名称列表]
{
	枚举值;
	变量成员声明及初始化;
	方法声明及方法体;
}

简单的例子:

/**
 * 简单的枚举类型举例
 * @author wangbaofu
 * 
 */
public class ScoreTester {
	public static void main(String[] args) {
		giveScore(Score.EXCELLENT);
	}
	private static void giveScore(Score s) {
		switch (s) {
		case EXCELLENT:
			System.out.println("Excellent");
			break;
		case QUALIFIED:
			System.out.println("Qualified");
			break;
		case FAILED:
			System.out.println("Failed");
			break;
		default:
			break;
		}
	}
}
enum Score {
	EXCELLENT, QUALIFIED, FAILED;
}
/*
 * 输出 Excellent
 * */

通过jad反编译一下:

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ScoreTester.java

package enum_;


final class Score extends Enum
{

    private Score(String s, int i)
    {
        super(s, i);
    }

    public static Score[] values()
    {
        Score ascore[];
        int i;
        Score ascore1[];
        System.arraycopy(ascore = ENUM$VALUES, 0, ascore1 = new Score[i = ascore.length], 0, i);
        return ascore1;
    }

    public static Score valueOf(String s)
    {
        return (Score)Enum.valueOf(enum_/Score, s);
    }

    public static final Score EXCELLENT;
    public static final Score QUALIFIED;
    public static final Score FAILED;
    public static final Score ADD;
    private static final Score ENUM$VALUES[];

    static 
    {
        EXCELLENT = new Score("EXCELLENT", 0);
        QUALIFIED = new Score("QUALIFIED", 1);
        FAILED = new Score("FAILED", 2);
        ADD = new Score("ADD", 3);
        ENUM$VALUES = (new Score[] {
            EXCELLENT, QUALIFIED, FAILED, ADD
        });
    }
}

枚举的特点:

  • 枚举定义实际上是定义了一个类
  • 所有枚举类型都隐含继承(扩展)自Java.lang.Enum,因此枚举类型不能再继承任何其它类
  • 枚举类型中的类可以包括方法和变量
  • 枚举类型的构造方法必须是包内私有或者私有的。定义在枚举开头的常量会自动创建,不能显示地调用枚举类的构造方法。

枚举类型的默认方法

  • 静态的values()方法用于获得枚举类型的枚举值的数组
  • toString方法返回枚举值的字符串描述
  • valueOf方法将以字符串形式表示的枚举值转化为枚举类型的对象
  • Ordinal方法获得对象在枚举类型中的位置索引

最佳实践 (该部分内容为《编写高质量代码之java》学习笔记)

下边贴一下jdk中Enum的源码

/*
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.lang;

import java.io.Serializable;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;

/**
 * This is the common base class of all Java language enumeration types.
 *
 * More information about enums, including descriptions of the
 * implicitly declared methods synthesized by the compiler, can be
 * found in section 8.9 of
 * <cite>The Java&trade; Language Specification</cite>.
 *
 * <p> Note that when using an enumeration type as the type of a set
 * or as the type of the keys in a map, specialized and efficient
 * {@linkplain java.util.EnumSet set} and {@linkplain
 * java.util.EnumMap map} implementations are available.
 *
 * @param <E> The enum type subclass
 * @author  Josh Bloch
 * @author  Neal Gafter
 * @see     Class#getEnumConstants()
 * @see     java.util.EnumSet
 * @see     java.util.EnumMap
 * @since   1.5
 */
public abstract class Enum<E extends Enum<E>>
        implements Comparable<E>, Serializable {
    /**
     * The name of this enum constant, as declared in the enum declaration.
     * Most programmers should use the {@link #toString} method rather than
     * accessing this field.
     */
    private final String name;

    /**
     * Returns the name of this enum constant, exactly as declared in its
     * enum declaration.
     *
     * <b>Most programmers should use the {@link #toString} method in
     * preference to this one, as the toString method may return
     * a more user-friendly name.</b>  This method is designed primarily for
     * use in specialized situations where correctness depends on getting the
     * exact name, which will not vary from release to release.
     *
     * @return the name of this enum constant
     */
    public final String name() {
        return name;
    }

    /**
     * The ordinal of this enumeration constant (its position
     * in the enum declaration, where the initial constant is assigned
     * an ordinal of zero).
     *
     * Most programmers will have no use for this field.  It is designed
     * for use by sophisticated enum-based data structures, such as
     * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
     */
    private final 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).
     *
     * Most programmers will have no use for this method.  It is
     * designed for use by sophisticated enum-based data structures, such
     * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
     *
     * @return the ordinal of this enumeration constant
     */
    public final int ordinal() {
        return ordinal;
    }

    /**
     * Sole constructor.  Programmers cannot invoke this constructor.
     * It is for use by code emitted by the compiler in response to
     * enum type declarations.
     *
     * @param name - The name of this enum constant, which is the identifier
     *               used to declare it.
     * @param ordinal - The ordinal of this enumeration constant (its position
     *         in the enum declaration, where the initial constant is assigned
     *         an ordinal of zero).
     */
    protected Enum(String name, int ordinal) {
        this.name = name;
        this.ordinal = ordinal;
    }

    /**
     * Returns the name of this enum constant, as contained in the
     * declaration.  This method may be overridden, though it typically
     * isn't necessary or desirable.  An enum type should override this
     * method when a more "programmer-friendly" string form exists.
     *
     * @return the name of this enum constant
     */
    public String toString() {
        return name;
    }

    /**
     * Returns true if the specified object is equal to this
     * enum constant.
     *
     * @param other the object to be compared for equality with this object.
     * @return  true if the specified object is equal to this
     *          enum constant.
     */
    public final boolean equals(Object other) {
        return this==other;
    }

    /**
     * Returns a hash code for this enum constant.
     *
     * @return a hash code for this enum constant.
     */
    public final int hashCode() {
        return super.hashCode();
    }

    /**
     * Throws CloneNotSupportedException.  This guarantees that enums
     * are never cloned, which is necessary to preserve their "singleton"
     * status.
     *
     * @return (never returns)
     */
    protected final Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    /**
     * Compares this enum with the specified object for order.  Returns a
     * negative integer, zero, or a positive integer as this object is less
     * than, equal to, or greater than the specified object.
     *
     * Enum constants are only comparable to other enum constants of the
     * same enum type.  The natural order implemented by this
     * method is the order in which the constants are declared.
     */
    public final int compareTo(E o) {
        Enum<?> other = (Enum<?>)o;
        Enum<E> self = this;
        if (self.getClass() != other.getClass() && // optimization
            self.getDeclaringClass() != other.getDeclaringClass())
            throw new ClassCastException();
        return self.ordinal - other.ordinal;
    }

    /**
     * Returns the Class object corresponding to this enum constant's
     * enum type.  Two enum constants e1 and  e2 are of the
     * same enum type if and only if
     *   e1.getDeclaringClass() == e2.getDeclaringClass().
     * (The value returned by this method may differ from the one returned
     * by the {@link Object#getClass} method for enum constants with
     * constant-specific class bodies.)
     *
     * @return the Class object corresponding to this enum constant's
     *     enum type
     */
    @SuppressWarnings("unchecked")
    public final Class<E> getDeclaringClass() {
        Class<?> clazz = getClass();
        Class<?> zuper = clazz.getSuperclass();
        return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
    }

    /**
     * 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.)
     *
     * <p>Note that for a particular enum type {@code T}, the
     * implicitly declared {@code 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 {@code public static T[] values()} method of that
     * type.
     *
     * @param <T> The enum type whose constant is to be returned
     * @param enumType the {@code Class} object of the enum type from which
     *      to return a constant
     * @param name the name of the constant to return
     * @return 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
     * @throws NullPointerException if {@code enumType} or {@code name}
     *         is null
     * @since 1.5
     */
    public static <T extends Enum<T>> T valueOf(Class<T> enumType,
                                                String name) {
        T result = enumType.enumConstantDirectory().get(name);
        if (result != null)
            return result;
        if (name == null)
            throw new NullPointerException("Name is null");
        throw new IllegalArgumentException(
            "No enum constant " + enumType.getCanonicalName() + "." + name);
    }

    /**
     * enum classes cannot have finalize methods.
     */
    protected final void finalize() { }

    /**
     * prevent default deserialization
     */
    private void readObject(ObjectInputStream in) throws IOException,
        ClassNotFoundException {
        throw new InvalidObjectException("can't deserialize enum");
    }

    private void readObjectNoData() throws ObjectStreamException {
        throw new InvalidObjectException("can't deserialize enum");
    }
}

推荐使用枚举定义常量

常量声明是每一个项目都不可或缺的,在Java 1.5之前,
我们只有两种方式的声明:类常量和接口常量,若在项目中
使用的是Java 1.5之前的版本基本上都是如此定义的。不过,
在1.5版以后有了改进,即新增了一种常量声明方式: 枚举声明常量
枚举声明常量的优点
**(1)**枚举常量更简单
**(2)**枚举常量属于稳态型
**(3)**枚举具有内置方法
是java.lang.Enum的子类,该基类提供了诸如获得排序值的ordinal方法、compareTo比较方法等,大大简化了常量的访问。
**(4)**枚举可以自定义方法
举常量不仅可以定义静态方法,还可以定义非静态方法,而且还能够从根本上杜绝常量类被实例化。

	package enumtest;
/**
 * 枚举的优势
 *
 */
public class TestSeason {
	public static void main(String[] args) {
//		通过values方法获得所有的枚举项
		for (Season s : Season.values()) {
			System.out.println(s);
		}
	}
}
enum Season{
	Spring,Summer,Autumn,Winter;
}
interface SeasonIntece{
	int Spring=0;
	int Summer=1;
	int Autumn=2;
	int Winter=3;
}

使用构造函数协助描述枚举项

枚举的属性:排序号,其默认值为0,1,2,3…
枚举描述:含义通过枚举的构造函数,声明每个枚举项(也就是枚举的实例)
必须具有属性和行为,这是对枚举的描述和补充,目的是使,枚举项表述的意义更加清晰准确

	enum Season {
	Spring("春"), Summer("夏"), Autumn("秋"), Winter("冬");
	private String desc;

	Season(String desc) {
		this.desc = desc;
	}
	// 自定义方法
	public static Season getComfortableSeason() {
		return Spring;
	}
//获取枚举描述
	public String getDesc() {
		return desc;
	}
}	

小心switch带来的空值异常,处理方法增加空判断

对于枚举类型很多的情况下,在default中添加: throw
new AssertionError(“没有该类型”);

使用valueOf前必须进行校验

	package enumtest;

import java.util.Arrays;
import java.util.List;

public class ValueOfTest {
	public static void main(String[] args) {
		List<String> params = Arrays.asList("EXCELLENT", "aDD");
		for (String name : params) {
			// 查找直面值与name相同的枚举 项
			
//			添加代码
			if(Score.contains(name)){
				Score s = Score.valueOf(name);
				if (s != null) {
					// 有该枚举项时
					System.out.println(s);
				} else {
					// 没有该枚举项时
					System.out.println("无相关枚举项");
				}
			}
			//			Score s = Score.valueOf(name);
//			if (s != null) {
//				// 有该枚举项时
//				System.out.println(s);
//			} else {
//				// 没有该枚举项时
//				System.out.println("无相关枚举项");
//			}
		}
	}

	enum Score {
		EXCELLENT, QUALIFIED, FAILED, ADD;
		// 是否包含枚举项
		public static boolean contains(String name) {
			// 所有的枚举值
			Score[] scores = values();
			// 遍历查找
			for (Score s : scores) {
				if (s.name().equals(name))
					return true;
			}
			return false;
		}

	}
}
/*
 * Exception in thread "main" java.lang.IllegalArgumentException: No enum
 * constant enumtest.ValueOfTest.Score.aDD
 * 
 * public static<T extends Enum<T>>T valueOf(Class<T>enumType, String name){
 * //通过反射,从常量列表中查找 T result=enumType.enumConstantDirectory().get(name);
 * if(result!=null) return result; if(name==null) throw new
 * NullPointerException("Name is null"); //最后排除无效参数异常 throw new
 * IllegalArgumentException("No enum const"+enumType+"."+name); }
 */

(1) 枚举非静态方法实现工厂方法模式
用枚举实现工厂方法模式更简洁

package enumtest;

/**
 * 工厂方法模式(Factory Method Pattern)是“创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其子类”。
 * 工厂方法模式在我们的开发工作中经常会用到。
 * 这是最原始的工厂方法模式,有两个产品:福特汽车和别克汽车,然后通过工厂方法模式来生产。有了工厂方法模式,我们就不用关心一辆车具体是怎么生成的了
 * ,只要告诉工厂“给我生产一辆福特汽车”就可以了
 */
// 抽象产品
interface Car {
};

// 具体产品类
class FordCar implements Car {
};

// 具体产品类
class BuickCar implements Car {
};

// 工厂类
public class CarFactory {
	public static Car createCar(Class<? extends Car> c) {
		try {
			return c.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {

			e.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) {
//		生产车辆
		Car cat =CarFactory.createCar(FordCar.class);
	}
}
package enumtest;
//枚举非静态方法实现工厂方法模式
public class CarFactoryEnum {
	public static void main(String[] args) {
//		生产汽车
		Car car = CarFactoryEnum1.BuickCar.create();
		
	}
}

enum CarFactoryEnum1 {
	// 定义工厂类能生产汽车的类型
	FordCar, BuickCar;
	// 生产汽车
	public Car create() {
		switch (this) {
		case FordCar:
			return new FordCar();
		case BuickCar:
			return new BuickCar();
		default:
			throw new AssertionError("无效参数");
		}
	}
}

(2)通过抽象方法生成产品

enum CarFactory3{
	FordCar{
		public Car create(){
			return new FordCar();
		}
	},BuickCar{
		public Car create(){
			return new BuickCar();
		}
	};
//	首先定义一个抽象制造方法create,然后每个枚举项自行实现。
	public abstract Car create();
}

用枚举类型的工厂方法模式有以下三个优点:
(1)避免错误调用的发生
(2)性能好,使用便捷
枚举类型的计算是以int类型的计算为基础的,这是最基本的操作,性能当
然会快,至于使用便捷,注意看客户端的调用,代码的字面意思就是“汽车
工厂,我要一辆别克汽车,赶快生产”。
(3)降低类间耦合
不管生产方法接收的是Class、String还是int的参数,都会成为客户端类的
负担,这些类并不是客户端需要的,而是因为工厂方法的限制必须输入的,
例如Class参数,对客户端main方法来说,它需要传递一个FordCar.class
参数才能生产一辆福特汽车,除了在create方法中传递该参数外,业务类
不需要改Car的实现类。这严重违背了
迪米特
原则(Law of Demeter,简称为
LoD
),也就是最少知识原则:一个对象应该对其他对象有最少的了解。
示例代码



	/**

	 * 支付类型

	 */

	public enum PayType{



		typeUnknow("未知支付类型" , -1), typeAlipay("支付宝支付" , 4), typeWechatPay("微信支付" , 5);

		private String name;

		private int value;



		// 构造方法

		private PayType(String name, int value) {

			this.name = name;

			this.value = value;

		}

	}

我是IT小王,如果喜欢我的文章,可以扫码关注我的微信公众号
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值