标签:
Java是一种面向对象的高级编程语言。它的出众之处就在于它的简洁。一个程序员所要做的就是创建类(Create Class)以及定义接口(Define Interface),如此而已。当然,这种简洁和优美是有代价的,比如失去了Enum这种广泛使用的数据类型就是一个不小的损失。在Java 1.5以前,程序员们不得不通过一些变通的方法来间接的解决这一问题。比如说,被普遍使用的整数枚举替代法和类型安全类替代法(Type safe Enum)。在正式讨论Java 1.5的枚举类型之前,让我们先简单回顾一下这两种枚举替代方法。
public class Season { public static final int SPRING = 0; public static final int SUMMER = 1; public static final int FALL = 2; public static final int WINTER = 3; } |
…… public void seasonTest(int season) { //season 应该是 Season.SPRING,Season.SUMMER, Season.FALL, Season.WINTER //但是我们无法保证这一点 …… } public void foo() { seasonTest(Season.SUMMER); //理想中的使用方法 seasonTest(5); //错误调用 } …… |
…… public String getSeasonString(int season) { If(season == Season.SPRING) return “Spring; else If(season == Season.SUMMRT) return “Summer; …… } |
public class Season { private Season(String s) { m_name = s; } public String toString() { return m_name; } private final String m_name; public static final Season SPRING = new Season("Spring"); public static final Season SUMMER = new Season("Summer"); public static final Season FALL = new Season("Fall"); public static final Season WINTER = new Season("Winter"); } |
public class ClientProgam { …. public String myFunction(Season season) { …… } } |
public class Season implements Comparable { private Season(String s) { m_ordinal = m_nextOrdinal++; m_name = s; } public String toString() { return m_name; } public String Name() { return m_name; } public int compareTo(Object obj) { return m_ordinal - ((Season)obj).m_ordinal; } public static final Season[] values() { return m_seasons; } public static Season valueOf(String s) { for(int i = 0; i < m_seasons.length; i++) if(m_seasons[i].Name().equals(s)) return m_seasons[i]; throw new IllegalArgumentException(s); } private final String m_name; private static int m_nextOrdinal = 0; private final int m_ordinal; public static final Season SPRING; public static final Season SUMMER; public static final Season FALL; public static final Season WINTER; private static final Season m_seasons[]; static { SPRING = new Season("Spring"); SUMMER = new Season("Summer"); FALL = new Season("Fall"); WINTER = new Season("Winter"); m_seasons = (new Season[] { SPRING, SUMMER, FALL, WINTER }); } } |
public enum Season { SPRING, SUMMER, FALL, WINTER } |
public final class Season extends Enum { public static final Season[] values() { return (Season[])$VALUES.clone(); } public static Season valueOf(String s) { Season aseason[] = $VALUES; int i = aseason.length; for(int j = 0; j < i; j++) { Season season = aseason[j]; if(season.name().equals(s)) return season; } throw new IllegalArgumentException(s); } private Season(String s, int i) { super(s, i); } public static final Season SPRING; public static final Season SUMMER; public static final Season FALL; public static final Season WINTER; private static final Season $VALUES[]; static { SPRING = new Season("SPRING", 0); SUMMER = new Season("SUMMER", 1); FALL = new Season("FALL", 2); WINTER = new Season("WINTER", 3); $VALUES = (new Season[] { SPRING, SUMMER, FALL, WINTER }); } } |
/* * @(#)Enum.java 1.12 04/06/08 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.lang; import java.io.Serializable; /** * This is the common base class of all Java language enumeration types. * * @author Josh Bloch * @author Neal Gafter * @version 1.12, 06/08/04 * @since 1.5 */ public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable { private final String name; public final String name() { return name; } private final int ordinal; public final int ordinal() { return ordinal; } protected Enum(String name, int ordinal) { this.name = name; this.ordinal = ordinal; } public String toString() { return name; } public final boolean equals(Object other) { return this==other; } public final int hashCode() { return System.identityHashCode(this); } protected final Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); } public final int compareTo(E o) { Enum other = (Enum)o; Enum self = this; if (self.getClass() != other.getClass() && // optimization self.getDeclaringClass() != other.getDeclaringClass()) throw new ClassCastException(); return self.ordinal - other.ordinal; } public final Class<E> getDeclaringClass() { Class clazz = getClass(); Class zuper = clazz.getSuperclass(); return (zuper == Enum.class) ? clazz : zuper; } 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); } } |
public enum Planet { MERCURY (3.303e+23, 2.4397e6), VENUS (4.869e+24, 6.0518e6), EARTH (5.976e+24, 6.37814e6), MARS (6.421e+23, 3.3972e6), JUPITER (1.9e+27, 7.1492e7), SATURN (5.688e+26, 6.0268e7), URANUS (8.686e+25, 2.5559e7), NEPTUNE (1.024e+26, 2.4746e7), PLUTO (1.27e+22, 1.137e6); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } private double mass() { return mass; } private double radius() { return radius; } // universal gravitational constant (m3 kg-1 s-2) public static final double G = 6.67300E-11; double surfaceGravity() { return G * mass / (radius * radius); } double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); } } |
public final class Planet extends Enum { public static final Planet[] values() { return (Planet[])$VALUES.clone(); } public static Planet valueOf(String s) { Planet aplanet[] = $VALUES; int i = aplanet.length; for(int j = 0; j < i; j++) { Planet planet = aplanet[j]; if(planet.name().equals(s)) return planet; } throw new IllegalArgumentException(s); } private Planet(String s, int i, double d, double d1) { super(s, i); mass = d; radius = d1; } private double mass() { return mass; } private double radius() { return radius; } double surfaceGravity() { return (6.6729999999999999E-011D * mass) / (radius * radius); } double surfaceWeight(double d) { return d * surfaceGravity(); } public static final Planet MERCURY; public static final Planet VENUS; public static final Planet EARTH; public static final Planet MARS; public static final Planet JUPITER; public static final Planet SATURN; public static final Planet URANUS; public static final Planet NEPTUNE; public static final Planet PLUTO; private final double mass; private final double radius; public static final double G = 6.6729999999999999E-011D; private static final Planet $VALUES[]; static { MERCURY = new Planet("MERCURY", 0, 3.3030000000000001E+023D, 2439700D); VENUS = new Planet("VENUS", 1, 4.8690000000000001E+024D, 6051800D); EARTH = new Planet("EARTH", 2, 5.9760000000000004E+024D, 6378140D); MARS = new Planet("MARS", 3, 6.4209999999999999E+023D, 3397200D); JUPITER = new Planet("JUPITER", 4, 1.9000000000000001E+027D, 71492000D); SATURN = new Planet("SATURN", 5, 5.6879999999999998E+026D, 60268000D); URANUS = new Planet("URANUS", 6, 8.686E+025D, 25559000D); NEPTUNE = new Planet("NEPTUNE", 7, 1.0239999999999999E+026D, 24746000D); PLUTO = new Planet("PLUTO", 8, 1.2700000000000001E+022D, 1137000D); $VALUES = (new Planet[] { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE, PLUTO }); } } |
public enum Operation { PLUS { double eval(double x, double y) { return x + y; } }, MINUS { double eval(double x, double y) { return x - y; } }, TIMES { double eval(double x, double y) { return x * y; } }, DIVIDE { double eval(double x, double y) { return x / y; } }; abstract double eval(double x, double y); } |
public abstract class Operation extends Enum { public static final Operation[] values() { return (Operation[])$VALUES.clone(); } public static Operation valueOf(String s) { Operation aoperation[] = $VALUES; int i = aoperation.length; for(int j = 0; j < i; j++) { Operation operation = aoperation[j]; if(operation.name().equals(s)) return operation; } throw new IllegalArgumentException(s); } private Operation(String s, int i) { super(s, i); } abstract double eval(double d, double d1); public static final Operation PLUS; public static final Operation MINUS; public static final Operation TIMES; public static final Operation DIVIDE; private static final Operation $VALUES[]; static { PLUS = new Operation("PLUS", 0) { double eval(double d, double d1) { return d + d1; } }; MINUS = new Operation("MINUS", 1) { double eval(double d, double d1) { return d - d1; } }; TIMES = new Operation("TIMES", 2) { double eval(double d, double d1) { return d * d1; } }; DIVIDE = new Operation("DIVIDE", 3) { double eval(double d, double d1) { return d / d1; } }; $VALUES = (new Operation[] { PLUS, MINUS, TIMES, DIVIDE }); } } |
public abstract class Operation { private final String m_name; Operation(String name) {m_name = name;} public static final Operation PLUS = new Operation("Plus"){ protected double eval(double x, double y){ return x + y; } } public static final Operation MINUS = new Operation("Minus"){ protected double eval(double x, double y){ return x - y; } } public static final Operation TIMES = new Operation("Times"){ protected double eval(double x, double y){ return x * y; } } public static final Operation DEVIDE = new Operation("Devide"){ protected double eval(double x, double y){ return x / y; } } abstract double eval (double x, double y); public String toString() {return m_name; } private static int m_nextOridnal = 0; private final int m_ordinal = m_nextOridnal++; private static final Operation[] VALUES = { PLUS, MINUS, TIMES, DEVIDE }; } |
public class Operation { private final String m_name; Operation(String name) {m_name = name;} public static final Operation PLUS = new Operation("Plus"); public static final Operation MINUS = new Operation("Minus"); public static final Operation TIMES = new Operation("Times"); public static final Operation DEVIDE = new Operation("Devide"); public double eval (double x, double y){ if(this == Operation.PLUS){ return x + y; } else if(this == Operation.MINUS){ return x - y; } else if(this == Operation.TIMES){ return x * y; } else if(this == Operation.DEVIDE){ return x / y; } return -1; } public String toString() {return m_name; } private static int m_nextOridnal = 0; private final int m_ordinal = m_nextOridnal++; private static final Operation[] VALUES = { PLUS, MINUS, TIMES, DEVIDE }; } |
public class EnumTest { public enum Grade {A,B,C,D,E,F,Incomplete } private Grade m_grade; public EnumTest(Grade grade) { this.m_grade = grade; testing(); } private void testing(){ switch(this.m_grade){ case A: System.out.println(Grade.A.toString()); break; case B: System.out.println(Grade.B.toString()); break; case C: System.out.println(Grade.C.toString()); break; case D: System.out.println(Grade.D.toString()); break; case E: System.out.println(Grade.E.toString()); break; case F: System.out.println(Grade.F.toString()); break; case Incomplete: System.out.println(Grade.Incomplete.toString()); break; } } public static void main(String[] args){ new EnumTest(Grade.A); } } |
…… //从略 private void testing() { static class _cls1 { static final int $SwitchMap$EnumTest1$Grade[]; static { $SwitchMap$EnumTest1$Grade = new int[Grade.values().length]; try { $SwitchMap$EnumTest1$Grade[Grade.A.ordinal()] = 1; } catch(NoSuchFieldError nosuchfielderror) { } try { $SwitchMap$EnumTest1$Grade[Grade.B.ordinal()] = 2; } catch(NoSuchFieldError nosuchfielderror1) { } try { $SwitchMap$EnumTest1$Grade[Grade.C.ordinal()] = 3; } catch(NoSuchFieldError nosuchfielderror2) { } try { $SwitchMap$EnumTest1$Grade[Grade.D.ordinal()] = 4; } catch(NoSuchFieldError nosuchfielderror3) { } try { $SwitchMap$EnumTest1$Grade[Grade.E.ordinal()] = 5; } catch(NoSuchFieldError nosuchfielderror4) { } try { $SwitchMap$EnumTest1$Grade[Grade.F.ordinal()] = 6; } catch(NoSuchFieldError nosuchfielderror5) { } try { $SwitchMap$EnumTest1$Grade[Grade.Incomplete.ordinal()] = 7; } catch(NoSuchFieldError nosuchfielderror6) { } } } switch(_cls1..SwitchMap.EnumTest1.Grade[m_grade.ordinal()]) { case 1: // ‘/001‘ System.out.println(Grade.A.toString()); break; case 2: // ‘/002‘ System.out.println(Grade.B.toString()); break; case 3: // ‘/003‘ System.out.println(Grade.C.toString()); break; case 4: // ‘/004‘ System.out.println(Grade.D.toString()); break; case 5: // ‘/005‘ System.out.println(Grade.E.toString()); break; case 6: // ‘/006‘ System.out.println(Grade.F.toString()); break; case 7: // ‘/007‘ System.out.println(Grade.Incomplete.toString()); break; } } |
public class MyEnum extends Enum { protected Enum(String name, int ordinal); protected Object clone( ); } |
public enum OperationExt extends Operation { LOG { double eval(double x, double y) { return Math.log(y) / Math.log(x);} }, POWER { double eval(double x, double y) { return Math.power(x,y);} }, } |
abstract class OperationExt extends Opetation { private final String m_name; Operation(String name) {m_name = name;} public static final LOG = new Operation("Log"){ protected double eval(double x, double y){ return Math.log(y) / Math.log(x); } } public static final POWER = new Operation("Power"){ protected double eval(double x, double y){ return Math.log(x,y); } } protected double eval (double x, double y); public String toString() {return m_name; } private static int m_nextOridnal = 0; private final int m_ordinal = m_nextOridnal++; private static final Operation[] VALUES = { LOG, POWER; } } |
标签:
原文地址:http://blog.csdn.net/huzhigenlaohu/article/details/51524129