ºìÁªLinuxÃÅ»§
Linux°ïÖú

JAVA»ù´¡£ºÔÙ̸ÔÚJavaÖÐʹÓÃö¾Ù

·¢²¼Ê±¼ä:2006-10-10 00:54:57À´Ô´:ºìÁª×÷Õß:hfh08
´ÓC++תµ½JavaÉϵijÌÐòÔ±Ò»¿ªÊ¼×ÜÊǶÔJavaÓв»ÉÙ±§Ô¹£¬ÆäÖÐûÓÐö¾Ù¾ÍÊÇÒ»¸ö±È½ÏÍ»³öµÄÎÊÌâ¡£ÄÇôΪʲôJava²»Ö§³Öö¾ÙÄØ£¿´Ó³ÌÐòÓïÑԵĽǶȽ²£¬Ö§³Öö¾ÙÒâζ×ÅʲôÄØ£¿ÎÒÃÇÄܲ»ÄÜÕÒµ½Ò»ÖÖ·½·¨Âú×ãC++³ÌÐòÔ±¶Ôö¾ÙµÄÒªÇóÄØ£¿ÄÇôÏÖÔÚ¾ÍÈÃÎÒÃÇÒ»ÆðÀ´Ì½ÌÖÒ»ÏÂÕâ¸öÎÊÌâ¡£

ö¾ÙÀàÐÍ£¨Enumerated Types£©

ÈÃÎÒÃÇÏÈ¿´ÏÂÃæÕâÒ»¶ÎС³ÌÐò£º

enum Day {SUNDAY, MONDAY, TUESDAY,
WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};



ÕâÖÖÉêÃ÷ÌṩÁËÒ»ÖÖÓû§ÓѺõıäÁ¿¶¨ÒåµÄ·½·¨£¬Ëüö¾ÙÁËÕâÖÖÊý¾ÝÀàÐÍËùÓпÉÄܵÄÖµ£¬¼´ÐÇÆÚÒ»µ½ÐÇÆÚÌì¡£Å׿ª¾ßÌå±à³ÌÓïÑÔÀ´¿´£¬Ã¶¾ÙËù¾ßÓеĺËÐŦÄÜÓ¦¸ÃÊÇ:


ÀàÐÍ°²È«£¨Type Safety£©


½ô´ÕÓÐЧµÄö¾ÙÊýÖµ¶¨Òå (Compact, Efficient Declaration of Enumerated Values)


ÎÞ·ìµÄºÍ³ÌÐòÆäËü²¿·ÖµÄ½»»¥²Ù×÷£¨Seamless integration with other language features£©


ÔËÐеĸßЧÂÊ£¨Runtime efficiency£©

ÏÖÔÚÎÒÃǾÍÕ⼸¸öÌصãÖðÒ»ÌÖÂÛһϡ£

1. ÀàÐÍ°²È«

ö¾ÙµÄÉêÃ÷´´½¨ÁËÒ»¸öеÄÀàÐÍ¡£Ëü²»Í¬ÓÚÆäËûµÄÒÑÓÐÀàÐÍ£¬°üÀ¨Ô­Ê¼ÀàÐÍ£¨ÕûÊý£¬¸¡µãÊýµÈµÈ£©ºÍµ±Ç°×÷ÓÃÓò£¨Scope£©ÄÚµÄÆäËüµÄö¾ÙÀàÐÍ¡£µ±Äã¶Ôº¯ÊýµÄ²ÎÊý½øÐи³Öµ²Ù×÷µÄʱºò£¬ÕûÊýÀàÐͺÍö¾ÙÀàÐÍÊDz»ÄÜ»¥»»µÄ£¨³ý·ÇÊÇÄã½øÐÐÏÔʽµÄÀàÐÍת»»£©£¬±àÒëÆ÷½«Ç¿ÖÆÕâÒ»µã¡£±ÈÈç˵£¬ÓÃÉÏÃæÉêÃ÷µÄö¾Ù¶¨ÒåÕâÑùÒ»¸öº¯Êý£º

public void foo(Day);



Èç¹ûÄãÓÃÕûÊýÀ´µ÷ÓÃÕâ¸öº¯Êý£¬±àÒëÆ÷»á¸ø³ö´íÎóµÄ¡£

foo(4); // compilation error



Èç¹û°´ÕÕÕâ¸ö±ê×¼£¬ÄÇôPascal, Ada, ºÍC++ÊÇÑϸñÒâÒåÉϵÄÖ§³Öö¾Ù£¬¶øCÓïÑÔ¶¼²»ÊÇ¡£

2. ½ô´ÕÓÐЧµÄö¾ÙÊýÖµ¶¨Òå

¶¨Òåö¾ÞµÄ³ÌÐòÓ¦¸ÃºÜ¼òµ¥¡£±ÈÈç˵£¬ÔÚJavaÖÐÎÒÃÇÓÐÕâÑùÒ»ÖÖ"׼ö¾Ù"µÄ¶¨Òå·½·¨£º

public static final int SUNDAY = 0;
public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 3;
public static final int THURSDAY = 4;
public static final int FRIDAY = 5;
public static final int SATURDAY = 6;



ÕâÖÖ¶¨Òå¾ÍËƺõ²»¹»¼ò½à¡£Èç¹ûÓдóÁ¿µÄÊý¾ÝÒª¶¨Ò壬ÕâÒ»µã¾ÍÓÈΪÖØÒª£¬ÄãÒ²¾Í»á¸ÐÊܸüÉî¡£ËäÈ»ÕâÒ»µã²»ÈçÆäËûÁíÍâ3µãÖØÒª£¬µ«ÎÒÃÇ×ÜÊÇÏ£ÍûÉêÃ÷Äܾ¡¿ÉÄܵļò½à¡£

3. ÎÞ·ìµÄºÍ³ÌÐòÆäËü²¿·ÖµÄ½»»¥²Ù×÷

ÓïÑÔµÄÔËËã·û£¬È縳ֵ£¬ÏàµÈ/´óÓÚ/СÓÚÅж϶¼Ó¦¸ÃÖ§³Öö¾Ù¡£Ã¶¾Ù»¹Ó¦¸ÃÖ§³ÖÊý×éϱêÒÔ¼°switch/caseÓï¾äÖÐÓÃÀ´¿ØÖÆÁ÷³ÌµÄ²Ù×÷¡£±ÈÈ磺

for (Day d = SUNDAY; d <= SATURDAY; ++d) {
switch(d) {
case MONDAY: ...;
break;
case TUESDAY: ...;
break;
case WEDNESDAY: ...;
break;
case THURSDAY: ...;
break;
case FRIDAY: ...;
break;
case SATURDAY:
case SUNDAY: ...;
}
}



ÒªÏëÈÃÕâ¶Î³ÌÐò¹¤×÷£¬ÄÇôö¾Ù±ØÐëÊÇÕûÊý³£Êý£¬¶ø²»ÄÜÊǶÔÏó£¨objects£©¡£JavaÖÐÄã¿ÉÒÔÓÃequals() »òÊÇ compareTo() º¯ÊýÀ´½øÐжÔÏóµÄ±È½Ï²Ù×÷£¬µ«ÊÇËüÃǶ¼²»Ö§³ÖÊý×éϱêºÍswitchÓï¾ä¡£

4. ÔËÐеĸßЧÂÊ

ö¾ÙµÄÔËÐÐЧÂÊÓ¦¸ÃºÍԭʼÀàÐ͵ÄÕûÊýÒ»Ñù¸ß¡£ÔÚÔËÐÐʱ²»Ó¦¸ÃÓÉÓÚʹÓÃÁËö¾Ù¶øµ¼ÖÂÐÔÄܱÈʹÓÃÕûÊýÓÐϽµ¡£

Èç¹ûÒ»ÖÖÓïÑÔÂú×ãÕâËĵãÒªÇó£¬ÄÇôÎÒÃÇ¿ÉÒÔ˵ÕâÖÖÓïÑÔÊÇÕæÕýµÄÖ§³Öö¾Ù¡£±ÈÈçÇ°ÃæËù˵µÄPascal, Ada, ºÍC++¡£ºÜÃ÷ÏÔ£¬Java²»ÊÇ¡£

JavaµÄ´´Ê¼ÈËJames GoslingÊǸö×ÊÉîµÄC++³ÌÐòÔ±£¬ËûºÜÇå³þʲôÊÇö¾Ù¡£µ«ËƺõËûÓÐÒâµÄɾ³ýÁËJavaµÄö¾ÙÄÜÁ¦¡£ÆäÔ­ÒòÎÒÃDz»µÃ¶øÖª¡£¿ÉÄÜÊÇËûÏëÇ¿µ÷ºÍ¹ÄÀøʹÓöà̬ÐÔ£¨polymorphism£©£¬²»¹ÄÀøʹÓöàÖØ·ÖÖ§¡£¶ø¶àÖØ·ÖÖ§ÍùÍùÊǺÍö¾ÙÁªºÏʹÓõġ£²»¹ÜËûµÄ³õÖÔÈçºÎ£¬ÎÒÃÇÔÚJavaÖÐÈÔÈ»ÐèҪö¾Ù¡£

JavaÖеļ¸ÖÖ"׼ö¾Ù"ÀàÐÍ

ËäÈ»Java ²»Ö±½ÓÖ§³ÖÓû§¶¨ÒåµÄö¾Ù¡£µ«ÊÇÔÚʵ¼ùÖÐÈËÃÇ»¹ÊÇ×ܽá³öһЩö¾ÙµÄÌæ´úÆ·¡£

µÚÒ»ÖÖÌæ´úÆ·¿ÉÒÔ½âÊÍΪ"ÕûÊý³£Êýö¾Ù"¡£ÈçÏÂËùʾ£º

public static final int SUNDAY = 0;
public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 3;
public static final int THURSDAY = 4;
public static final int FRIDAY = 5;
public static final int SATURDAY = 6;



ÕâÖÖ·½·¨¿ÉÒÔÈÃÎÒÃÇʹÓøüÓÐÒâÒåµÄ±äÁ¿Ãû¶ø²»ÊÇÖ±½Ó³àÂãÂãµÄÕûÊýÖµ¡£ÕâÑùʹµÃÔ´³ÌÐòµÄ¿É¶ÁÐԺͿÉά»¤ÐÔ¸üºÃһЩ¡£ÕâЩ¶¨Òå¿ÉÒÔ·ÅÔÚÈκÎÀàÖС£¿ÉÒÔºÍÆäËüµÄ±äÁ¿ºÍ·½·¨»ìÔÚÒ»Æð¡£Ò²¿ÉÒÔµ¥¶À·ÅÔÚÒ»¸öÀàÖС£Èç¹ûÄãÑ¡Ôñ½«Æäµ¥¶À·ÅÔÚÒ»¸öÀàÖУ¬ÄÇôÒýÓõÄʱºòҪעÒâÓï·¨¡£±ÈÈç"Day.MONDAY."¡£Èç¹ûÄãÏëÔÚÒýÓõÄʱºòÊ¡Ò»µãÊ£¬ÄÇôÄã¿ÉÒÔ½«Æä·ÅÔÚÒ»¸ö½Ó¿ÚÖУ¨interface£©£¬ÆäËüÀàÖ»ÒªÉêÃ÷ʵÏÖ£¨implement£©Ëü¾Í¿ÉÒԱȽϷ½±ãµÄÒýÓᣱÈÈçÖ±½ÓʹÓÃMONDAY¡£¾ÍJava½Ó¿ÚµÄʹÓÃÄ¿µÄ¶øÑÔ£¬ÕâÖÖÓ÷¨ÓÐЩƫ£¬²»ÓÃÒ²°Õ£¡

ÕâÖÖ·½·¨ÏÔÈ»Âú×ãÁËÌõ¼þ3ºÍ4£¬¼´ÓïÑԵļ¯³ÉºÍÖ´ÐÐЧÂÊ£¨Ã¶¾Ù¾ÍÊÇÕûÊý£¬Ã»ÓÐЧÂÊËðʧ£©¡£µ«ÊÇËûÈ´²»ÄÜÂú×ãÌõ¼þ1ºÍ2¡£ËüµÄ¶¨ÒåÓÐЩ?࣬¸üÖØÒªµÄÊÇËü²»ÊÇÀàÐÍ°²È«µÄ¡£ÕâÖÖ·½·¨ËäÈ»Æձ鱻Java³ÌÐòÔ±²ÉÓ㬵«Ëü²»ÊÇÒ»ÖÖö¾ÙµÄÁ¼ºÃÌæ´úÆ·¡£

µÚ¶þÖÖ·½·¨ÊDZ»Ò»Ð©ÓÐÃûµÄר¼Ò¾­³£Ìá¼°µÄ¡£ÎÒÃÇ¿ÉÒÔ³ÆËüΪ"¶ÔÏóö¾Ù"¡£¼´ÎªÃ¶¾Ù´´½¨Ò»¸öÀ࣬ȻºóÓù«ÓõĸÃÀàµÄ¶ÔÏóÀ´±í´ïÿһ¸öö¾ÙµÄÖµ¡£ÈçÏÂËùʾ£º

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.Serializable;
import java.io.InvalidObjectException;
public final class Day implements Comparable, Serializable {
private static int size = 0;
private static int nextOrd = 0;
private static Map nameMap = new HashMap(10);
private static Day first = null;
private static Day last = null;
private final int ord;
private final String label;
private Day prev;
private Day next;
public static final Day SUNDAY = new Day("SUNDAY");
public static final Day MONDAY = new Day("MONDAY");
public static final Day TUESDAY = new Day("TUESDAY");
public static final Day WEDNESDAY = new Day("WEDNESDAY");
public static final Day THURSDAY = new Day("THURSDAY");
public static final Day FRIDAY = new Day("FRIDAY");
public static final Day SATURDAY = new Day("SATURDAY");
/**
* ÓÃËù¸øµÄ±êÇ©´´½¨Ò»¸öеÄday.
* (Uses default value for ord.)
*/
private Day(String label) {
this(label, nextOrd);
}
/**
* Constructs a new Day with its label and ord value.
*/
private Day(String label, int ord) {
this.label = label;
this.ord = ord;
++size;
nextOrd = ord + 1;
nameMap.put(label, this);
if (first == null)
first = this;
if (last != null) {
this.prev = last;
last.next = this;
}
last = this;
}
/**
* Compares two Day objects based on their ordinal values.
* Satisfies requirements of interface java.lang.Comparable.
*/
public int compareTo(Object obj) {
return ord - ((Day)obj).ord;
}
/**
* Compares two Day objects for equality. Returns true
* only if the specified Day is equal to this one.
*/
public boolean equals(Object obj) {
return super.equals(obj);
}
/**
* Returns a hash code value for this Day.
*/
public int hashCode() {
return super.hashCode();
}
/**
* Resolves deserialized Day objects.
* @throws InvalidObjectException if deserialization fails.
*/
private Object readResolve() throws InvalidObjectException {
Day d = get(label);
if (d != null)
return d;
else {
String msg = "invalid deserialized object: label = ";
throw new InvalidObjectException(msg + label);
}
}
/**
* Returns Day with the specified label.
* Returns null if not found.
*/
public static Day get(String label) {
return (Day) nameMap.get(label);
}
/**
* Returns the label for this Day.
*/
public String toString() {
return label;
}
/**
* Always throws CloneNotSupportedException; guarantees that
* Day objects are never cloned.
*
* @return (never returns)
*/
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
/**
* Returns an iterator over all Day objects in declared order.
*/
public static Iterator iterator() {
// anonymous inner class
return new Iterator()
{
private Day current = first;
public boolean hasNext() {
return current != null;
}
public Object next() {
Day d = current;
current = current.next();
return d;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
/**
* Returns the ordinal value of this Day.
*/
public int ord() {
return this.ord;
}
/**
* Returns the number of declared Day objects.
*/
public static int size() {
return size;
}
/**
* Returns the first declared Day.
*/
public static Day first() {
return first;
}
/**
* Returns the last declared Day.
*/
public static Day last() {
return last;
}
/**
* Returns the previous Day before this one in declared order.
* Returns null for the first declared Day.
*/
public Day prev() {
return this.prev;
}
/**
* Returns the next Day after this one in declared order.
* Returns null for the last declared Day.
*/
public Day next() {
return this.next;
}
}



ö¾ÙÖµ±»¶¨ÒåΪ¹«Óþ²Ì¬¶ÔÏó£¨public static object£©¡£´ËÍâ¸ÃÀຬÓÐ˽Óй¹Ô캯Êý£»Ò»¸öÑ­»·Æ÷£¨Iterator£©ÓÃÒÔ±éÀúËùÓеÄÖµ£»Ò»Ð©JavaÖг£Óõĺ¯Êý£¬ÈçtoString()£¬equals()ºÍcompareTo()£¬ÒÔ¼°Ò»Ð©·½±ã¿Í»§³ÌÐòµ÷Óõĺ¯Êý£¬Èçord()£¬prev()£¬next()£¬first()ºÍ last()¡£

ÕâÖÖʵÏÖ·½·¨ÓкܺõÄÀàÐÍ°²È«ºÍÔËÐÐЧÂÊ£¨Ìõ¼þ1ºÍ4£©¡£µ«ÊÇÈ¥²»Âú×ãÌõ¼þ2ºÍ3¡£Ê×ÏÈËüµÄ¶¨Òå±È½Ï·±Ëö£¬´ó¶àÊý³ÌÐòÔ±Ò²ÐíÒòΪÕâ¸ö¶ø²»È¥Ê¹ÓÃËü£»Í¬Ê±Ëû»¹²»¿ÉÒÔ±»ÓÃ×÷Êý×éϱê»òÊÇÓÃÔÚswitch/caseÓï¾ä¡£ÕâÔÚÒ»¶¨³Ì¶ÈÉϽµµÍÁËËûµÄʹÓõĹ㷺ÐÔ¡£

¿´ÆðÀ´£¬Ã»ÓÐÒ»ÖÖÌæ´úÆ·ÊÇÀíÏëµÄ¡£ÎÒÃÇËäȻûÓÐȨÀûÐÞ¸ÄJavaÓïÑÔ£¬µ«ÊÇÎÒÃÇÒ²Ðí¿ÉÒÔÏëһЩ°ì·¨À´¿Ë·þ"¶ÔÏóö¾Ù"µÄȱµã£¬Ê¹Ëü³ÉΪºÏ¸ñµÄö¾ÙÌæ´úÆ·¡£
Ò»¸öʵÏÖö¾ÙµÄ΢ÐÍÓïÑÔ£¨£ÁMini-Language for Enums£©

¼ÙÈçÎÒ·¢Ã÷Ò»ÖÖö¾ÙרÓõÄ΢ÐÍÓïÑÔ£¨ÇÒ½ÐËüjEnum£©£¬ËüרÃÅÓÃÀ´ÉêÃ÷ö¾Ù¡£È»ºóÎÒÔÙÓÃÒ»¸öÌØÊâµÄ"·­Òë"³ÌÐò½«ÎÒÓÃÕâÖÖÓïÑÔ¶¨ÒåµÄö¾Ùת»¯Îª¶ÔÓ¦µÄ"¶ÔÏóö¾Ù"¶¨Ò壬ÄDz»ÊǾͽâ¾öÁË"¶ÔÏóö¾Ù"¶¨Ò帴ÔÓµÄÎÊÌâÁËÂð¡£µ±È»ÎÒÃǺÜÈÝÒ×ÈÃÕâ¸ö"·­Òë"³ÌÐò¶à×öһЩ¹¤×÷¡£±ÈÈç¼ÓÈëPackageÉêÃ÷£¬¼ÓÈë³ÌÐò×¢ÊÍ£¬ËµÃ÷ÕûÊýÖµºÍ¸Ã¶ÔÏóµÄ×Ö·û´®±êÇ©Ãû³ÆµÈµÈ¡£ÈÃÎÒÃÇ¿´ÏÂÃæÕâÑùÒ»¸öÀý×Ó£º

package com.softmoore.util;
/**
* Various USA coins
*/
enum Coin { PENNY("penny") = 1, NICKEL("nickel") = 5, DIME("dime") = 10,
QUARTER("quarter") = 25, HALF_DOLLAR("half dollar") = 50 };



ËäÈ»"ÕûÊý³£Êýö¾Ù"ÔÚÓÐЩÇé¿öÏÂÓŵã±È½ÏÏÔÖø¡£µ«ÊÇ×ÜÌåÉϽ²"¶ÔÏóö¾Ù"ÌṩµÄÀàÐÍ°²È«»¹ÊǸüΪÖØÒªµÄ£¬Ïà±ÈÖ®ÏÂÄÄЩȱµã»¹ÊDZȽϴÎÒªµÄ¡£ÏÂÃæÎÒÃÇ´ó¸Å½²Ò»ÏÂjEnum£¬Ê¹ÓÃËüÎÒÃÇÓÖ¿ÉÒԵõ½½ô´ÕºÍÓÐЧµÄö¾ÙÉêÃ÷ÕâÒ»Ìص㣬Ҳ¾ÍÊÇÎÒÃÇÇ°ÃæÌáµ½µÄÌõ¼þ2¡£

ÊìϤ±àÒëÆ÷µÄÅóÓÑ¿ÉÄܸüÈÝÒ×Àí½âÏÂÃæÕâÒ»¶ÎjEnum΢ÐÍÓïÑÔ¡£

compilationUnit = ( packageDecl )? ( docComment )? enumTypeDecl .
packageDecl = "package" packagePath ";" .
packagePath = packageName ( "." packageName )* .
docComment = "/**" commentChars "*/" .
enumTypeDecl = "enum" enumTypeName "{" enumList "}" ";" .
enumList = enumDecl ( "," enumDecl )* .
enumDecl = enumLiteral ( "(" stringLiteral ")" )? ( "=" intLiteral )? .
packageName = identifier .
enumTypeName = identifier .
enumLiteral = identifier .
commentChars = any-char-sequence-except-"*/"



ÕâÖÖÓï·¨ÔÊÐíÔÚ¿ªÊ¼ÉêÃ÷package£¬¿´ÆðÀ´ºÍJavaÓïÑÔ»¹Í¦Ïñ¡£Äã¿ÉÒÔÔö¼ÓһЩjavadocµÄ×¢½â£¬µ±È»Õâ²»ÊDZØÐëµÄ¡£Ã¶¾ÙÀàÐ͵ÄÉêÃ÷ÒԹؼü×Ö"enum"¿ªÍ·£¬Ã¶¾ÙµÄÖµ·ÅÔÚ»¨À¨ºÅÖÐ{}£¬¶à¸öÖµÖ®¼äÓöººÅ·Ö¿ª¡£Ã¿Ò»¸öÖµµÄÉêÃ÷°üÀ¨Ò»¸ö±ê×¼µÄJava±äÁ¿Ãû£¬Ò»¸ö¿ÉÑ¡µÄ×Ö·û´®±êÇ©£¬¿ÉÑ¡µÄµÈºÅ£¨=£©ºÍÒ»¸öÕûÊýÖµ¡£

Èç¹ûÄãÊ¡ÂÔÁË×Ö·û´®±êÇ©£¬ÄÇôö¾ÙµÄ±äÁ¿Ãû¾Í»á±»Ê¹Óã»Èç¹ûÄãÊ¡ÂÔÁ˵ȺźͺóÃæµÄÕûÊýÖµ£¬ÄÇôËü½«»á×Ô¶¯°´Ë³Ðò¸øÄãµÄö¾Ù¸³Öµ£¬Èç¹ûûÓÐʹÓÃÈκÎÊýÖµ£¬ÄÇôËü´ÓÁ㿪ʼÖð²½Ôö¼Ó£¨²½³¤Îª1£©¡£×Ö·û´®±êÇ©×÷ΪtoString()·½·¨·µ»ØÖµµÄÒ»²¿·Ö£¬¶øÕûÊýÖµÔò×÷Ϊord()·½·¨µÄ·µ»ØÖµ¡£ÈçÏÂÃæÕâ¶ÎÉêÃ÷£º

enum Color { RED("Red") = 2, WHITE("White") = 4, BLUE };




RED µÄ±êÇ©ÊÇ "Red"£¬ÖµÎª 2 £»


WHITEµÄ±êÇ©ÊÇ"White"£¬ÖµÎª4£»


BLUEµÄ±êÇ©ÊÇ"BLUE" £¬ÖµÎª5 ¡£

ҪעÒâµÄÊÇÔÚJavaÖеı£Áô×ÖÔÚjEnumÒ²ÊDZ£ÁôµÄ¡£±ÈÈçÄã²»¿ÉÒÔʹÓÃthis×÷ΪpackageÃû£¬²»¿ÉÒÔÓÃforΪö¾ÙµÄ±äÁ¿ÃûµÈµÈ¡£Ã¶¾ÙµÄ±äÁ¿ÃûºÍ×Ö·û´®±êÇ©±ØÐëÊDz»Í¬µÄ£¬ÆäÕûÊýÖµÒ²±ØÐëÊÇÑϸñÏòÉÏÔö¼ÓµÄ£¬ÏóÏÂÃæÕâ¶ÎÉêÃ÷¾ÍÊDz»¶ÔµÄ£¬ÒòΪËüµÄ×Ö·û´®±êÇ©²»ÊÇΨһµÄ¡£

enum Color { RED("Red"), WHITE("BLUE"), BLUE };



ÏÂÃæÕâ¶ÎÉêÃ÷Ò²ÊDz»¶ÔµÄ£¬ÒòΪWHITE»á±»×Ô¶¯¸³Öµ2 £¬ºÍBLUEÓгåÍ»¡£

enum Color { RED = 1, WHITE, BLUE = 2 };



ÏÂÃæÕâÊÇÒ»¸ö¾ßÌåµÄʵÀý¡£Ëü½«»á±»"·­Òë"³ÌÐòʹÓã¬ÓÃÒÔת»»³ÉÎÒÃÇö¾ÙÉêÃ÷Ϊ¿É±àÒëµÄJavaÔ´³ÌÐò¡£

package com.softmoore.jEnum;
/**
* This class encapsulates the symbols (a.k.a. token types)
* of a language token.
*/
enum Symbol {
identifier,
enumRW("Reserved Word: enum"),
abstractRW("Reserved Word: abstract"),
assertRW("Reserved Word: assert"),
booleanRW("Reserved Word: boolean"),
breakRW("Reserved Word: break"),
byteRW("Reserved Word: byte"),
caseRW("Reserved Word: case"),
catchRW("Reserved Word: catch"),
charRW("Reserved Word: char"),
classRW("Reserved Word: class"),
constRW("Reserved Word: const"),
continueRW("Reserved Word: continue"),
defaultRW("Reserved Word: default"),
doRW("Reserved Word: do"),
doubleRW("Reserved Word: double"),
elseRW("Reserved Word: else"),
extendsRW("Reserved Word: extends"),
finalRW("Reserved Word: final"),
finallyRW("Reserved Word: finally"),
floatRW("Reserved Word: float"),
forRW("Reserved Word: for"),
gotoRW("Reserved Word: goto"),
ifRW("Reserved Word: if"),
implementsRW("Reserved Word: implements"),
importRW("Reserved Word: import"),
instanceOfRW("Reserved Word: instanceOf"),
intRW("Reserved Word: int"),
interfaceRW("Reserved Word: interface"),
longRW("Reserved Word: long"),
nativeRW("Reserved Word: native"),
newRW("Reserved Word: new"),
nullRW("Reserved Word: null"),
packageRW("Reserved Word: package"),
privateRW("Reserved Word: private"),
protectedRW("Reserved Word: protected"),
publicRW("Reserved Word: public"),
returnRW("Reserved Word: return"),
shortRW("Reserved Word: short"),
staticRW("Reserved Word: static"),
strictfpRW("Reserved Word: strictfp"),
superRW("Reserved Word: super"),
switchRW("Reserved Word: switch"),
synchronizedRW("Reserved Word: synchronized"),
thisRW("Reserved Word: this"),
throwRW("Reserved Word: throw"),
throwsRW("Reserved Word: throws"),
transientRW("Reserved Word: transient"),
tryRW("Reserved Word: try"),
voidRW("Reserved Word: void"),
volatileRW("Reserved Word: volatile"),
whileRW("Reserved Word: while"),
equals("="),
leftParen("("),
rightParen(")"),
leftBrace("{"),
rightBrace("}"),
comma(","),
semicolon(";"),
period("."),
intLiteral,
stringLiteral,
docComment,
EOF,
unknown
};



Èç¹û¶ÔDayµÄö¾ÙÉêÃ÷´æ·ÅÔÚDay.enumÎļþÖУ¬ÄÇôÎÒÃÇ¿ÉÒÔ½«Õâ¸öÎļþ·­Òë³ÉJavaÔ´³ÌÐò¡£

$ java -jar jEnum.jar Day.enum



·­ÒëµÄ½á¹û¾ÍÊÇDay.javaJavaÔ´³ÌÐò£¬ÄÚÈݺÍÎÒÃÇÇ°Ãæ½²µÄÒ»Ñù£¬»¹°üÀ¨³ÌÐò×¢Ê͵ÈÄÚÈÝ¡£Èç¹ûÏëÊ¡Ò»µãÊ£¬Äã¿ÉÒÔ½«ÉÏÃæ±È½Ï³¤µÄÃüÁîд³ÉÒ»¸öÅú´¦ÀíÎļþ»òÊÇUnix£¬LinuxÉϵÄshell script£¬ÄÇôÒÔºóʹÓõÄʱºò¾Í¿ÉÒÔ¼òµ¥Ò»Ð©£¬±ÈÈ磺

$ jec Day.enum



¹ØÓÚjEnumÓÐËĵã×¢ÒâÊÂÏîҪ˵Ã÷һϡ£

1£® ÉêÃ÷ÎļþÃû²»Ò»¶¨ºó׺Ϊ".enum."£¬ÆäËüºÏ·¨Îļþºó׺¶¼¿ÉÒÔ¡£

2£® Èç¹ûÎļþºó׺²»ÊÇ".enum."£¬ÄÇô·­Òë³ÌÐò½«Ê×ÏÈ°´¸ø³öµÄÎļþÃûÈ¥ËÑË÷£¬Èç¹ûûÓУ¬¾Í¼Ù¶¨¸ø³öµÄÎļþÃûÊÇÊ¡ÂÔÁË".enum."ºó׺µÄ¡£ÏñÕâÖÖÃüÁîÊÇ¿ÉÒԵģº

$ java -jar jEnum.jar Day



3£® Éú³ÉµÄJavaÔ´³ÌÐòÎļþÃûÊÇ°´ÕÕÉêÃ÷ÎļþÄڵĶ¨ÒåµÃ³öµÄ£¬¶ø²»ÊÇÒÀ¾ÝÉêÃ÷ÎļþµÄÃû³Æ¡£

4£® ·­Òë³ÌÐò»¹½ÓÊÜÒÔϼ¸¸ö¿ª¹Ø

-o Éú³É"¶ÔÏóö¾Ù"Ààö¾Ù£¬ÊÇȱʡֵ

-c Éú³É"ÕûÊý³£Êýö¾Ù"Ààö¾Ù£¬ÓÃÀàÀ´ÊµÏÖ

-i Éú³É"ÕûÊý³£Êýö¾Ù"Ààö¾Ù£¬ÓýӿÚÀ´ÊµÏÖ

ҪעÒâµÄÊÇ£¬-C¿ª¹ØËäÈ»Éú³É"ÕûÊý³£Êýö¾Ù"£¬µ«Ëüͬʱ»¹ÌṩÁËһЩ"¶ÔÏóö¾Ù"ÖÐËù¾ßÓеķ½·¨£¬Èçfirst()£¬ last()£¬toString(int n)£¬prev(int n)£¬ ºÍnext(int n)¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ