Java enum class enum

Source: Internet
Author: User

 Public Abstract classEnum<eextendsEnum<e>>ImplementsComparable<e>, Serializable {Private FinalString name; //Current enumeration constant name     Public FinalString name () {returnname; }        Private Final intordinal; //Current enumeration constant order, starting from 0     Public Final intordinal () {returnordinal; }        //Proprietary constructor that we cannot invoke.   The construction method is used for code emitted by the compiler that responds to the enumeration type declaration.     protectedEnum (String name,intordinal) {       This. Name =name;  This. Ordinal =ordinal; }        //returns the name of the enumeration constant, which returns a name value by default.  You can override this method to output a more friendly description.      PublicString toString () {returnname; }        //compares whether the current enumeration constant is equal to the specified object. Because enumeration constants are singleton, the = = operator is called directly.  Subclasses are not allowed to override this method.      Public Final Booleanequals (Object other) {return  This==Other ; }        //returns the hash code for the enumeration constant.  Consistent with equals, this method cannot be overridden.      Public Final inthashcode () {return Super. Hashcode (); }        //because enumeration constants are singleton, cloning is not allowed.     protected FinalObject Clone ()throwsclonenotsupportedexception {Throw Newclonenotsupportedexception (); }        //compares the enumeration constant and the size of the specified object. They are of the same type and return the size (small in front, large behind) according to their order in the enumeration declaration. Subclasses are not allowed to override this method     Public Final intcompareTo (E o) {Enum other=(Enum) o; Enum Self= This; if(Self.getclass ()! = Other.getclass () &&//optimizationSelf.getdeclaringclass ()! =Other.getdeclaringclass ())Throw Newclasscastexception (); returnSelf.ordinal-other.ordinal; }        //gets the class object of the enumeration type to which the enumeration constant belongs     Public FinalClass<e>Getdeclaringclass () {Class clazz=getclass (); Class Zuper=Clazz.getsuperclass (); return(Zuper = = Enum.)class) ?Clazz:zuper; }        //returns an enumeration constant of the specified enumeration type with the specified name. The name must exactly match the identifier used to declare the enumeration constants in this type.  Extra whitespace characters are not allowed.      Public Static<textendsEnum<t>> T valueOf (class<t>enumtype, String name) {T result=enumtype.enumconstantdirectory (). get (name); if(Result! =NULL)              returnresult; if(Name = =NULL)              Throw NewNullPointerException ("Name is null"); Throw NewIllegalArgumentException ("No enum Const" + Enumtype + "." +name); }        //do not allow deserialization of enumerated objects    Private voidReadObject (ObjectInputStream in)throwsIOException, ClassNotFoundException {Throw NewInvalidobjectexception ("Can ' t deserialize enum"); }        //do not allow deserialization of enumerated objects    Private voidReadobjectnodata ()throwsobjectstreamexception {Throw NewInvalidobjectexception ("Can ' t deserialize enum"); }        //An enumeration class cannot have a Finalize method, and subclasses cannot override the method    protected Final voidFinalize () {}}
 PackageTestenum; Public classTestenum { Public enumstats{Open,off,r,g,b, Huang, ff0000; }     Public enumcolor{} Public enumYes {A (0), B (1), C (2), D (3), F (4); PrivateInteger Code; Yes () {} Yes (intcode) {               This. Code =Code; }                 PublicInteger GetCode () {returnCode; }      }           Public enumWeek {mon{ PublicString tolocalestring () {return"Monday"; }             PublicString toLocaleString2 () {return"Monday"; }},tues{ PublicString tolocalestring () {return"Tuesday"; }},web{ PublicString tolocalestring () {return"Wednesday"; }},thur{ PublicString tolocalestring () {return"Thursday"; }},fri{ PublicString tolocalestring () {return"Friday"; }},sat{ PublicString tolocalestring () {return"Saturday"; }},sun{ PublicString tolocalestring () {return"Sunday";        }        };  Public AbstractString tolocalestring (); }     Public Static voidmain (String [] args) {System.out.println ("《------------------------------------------------------------------》"); System.out.println (stats.class);        System.out.println (Stats.off);        System.out.println (Stats.open);  for(intI=0;i<stats.values (). length;i++) {System.out.println ("Stats.values () [" +i+ "]:" +stats.values () [i]);        System.out.println (Stats.valueof (Stats.values () [I].tostring ()]); } System.out.println ("《------------------------------------------------------------------》"); System.out.println (Yes. )class);        System.out.println (YES.A);                System.out.println (Yes.b.getcode ());  for(intI=0;i<yes.values (). length;i++) {System.out.println ("Yes.values () [" +i+ "]:" +yes.values () [i]);        System.out.println (Yes.valueof (Yes.values () [I].tostring ()]); } System.out.println ("《------------------------------------------------------------------》"); System.out.println (Week.class);        System.out.println (Week.MON.FRI.MON);                System.out.println (Week.SUN.toLocaleString ());  for(intI=0;i<week.values (). length;i++) {System.out.println ("Week.values () [" +i+ "]:" +week.values () [i]);            System.out.println (Week.valueof (Week.values () [I].tostring ()]);        System.out.println (Week.valueof (Week.values () [I].tostring ()). toLocaleString ()); }    }}

Java enum class enum

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.