Enum class in Java

#notes

Enumeration class: limited objects

Custom enumeration class code:

public  class TestEnumeration { 

    public  static  void main (String [] args) {
         // method TODO automatically generated stubs 
        Season = Spring Season.SPRING; 
        spring.show (); 
        Season Winter = Season.WINTER; 
        winter.show (); 

    } 

} 

// enumeration class 
class Season {
     // provide attributes class declared as final private 
    private  final String seasonName;
     private  final String seasonDesc; 
    
    
    // properties that are final, initialization configuration 
    privateSeason (seasonName String, String seasonDesc) {
         the this .seasonName = seasonName;
         the this .seasonDesc = seasonDesc;     
    } 
    
    public String getSeasonName () {
         return seasonName; 
    } 

    public String getSeasonDesc () {
         return seasonDesc; 
    } 
    
    // Create enumeration object 
    public  static  Final Season = sPRING new new Season ( "spring", "which is spring" );  
     public  static  Final Season = wINTER new new Season ( "winter", "this is a winter" );  
     public static  Final Season = SUMMER new new Season ( "summer", "this summer" );  
     public  static  Final Season = AUTUMN new new Season ( "autumn", "This is the fall" );   
            
    public  void Show () { 
        System.out.println ( "this is a" + the this .getSeasonName ()); 
    } 
    
    
}

    
    
    

 

 

Use enum create enumerated classes:

{class TestEnumerationEnum public 

    public static void main (String [] args) {
        SeasonEnum = Spring SeasonEnum.SPRING;
        spring.show ();
        
        
        // method returns the object values enumeration class (back in array form)
        SeasonEnum [] = Sea SeasonEnum.values ();
        for (Object obj: Sea) {
            System.out.println (obj);
        }
        
        //.values(String name) returns the object enumeration class
        String str = "SPRING"; // not indiscriminate write, otherwise reported abnormal
        SeasonEnum Sea1 = SeasonEnum.valueOf (str);
        System.out.println (Sea1);  // return SeasonEnum [seasonName = spring, seasonDesc = this is the spring]
    }

}


create an enumeration type enum //
enum SeasonEnum {
    
    // uppermost enumeration class into the object class and with a custom simplified to:
    SPRING ( "Spring", "It's spring"),  
    WINTER ( "winter", "This is the winter"),
    SUMMER ( "summer", "this summer"),  
    AUTUMN ( "autumn", "This is the fall." );  
    
    
    // attribute provides classes declared as final Private
    Private final String seasonName;
    Private final String seasonDesc;
    
    
    // attribute declared final, the initialization configuration
    Private SeasonEnum (seasonName String, String seasonDesc) {
        this.seasonName = seasonName ;
        this.seasonDesc = seasonDesc;    
    }
    
    public String getSeasonName () {
        return seasonName;
    }

    public String getSeasonDesc () {
        return seasonDesc;
    }
            
    @Override
    public String toString () {
        return "SeasonEnum [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
    }

    public void show() {
        System.out.println("这是"+this.getSeasonName());
    }
    
    
}

 

 

Enumeration class that implements the interface:

public  class TestEnumerationInter { 

    public  static  void main (String [] args) { 
        SeasonEnum Winter = SeasonEnum.WINTER; 
        winter.show ();   // return this winter 
        
        SeasonEnum Sea = SeasonEnum.valueOf ( "SUMMER" ); 
        sea.show ( );    // this is a summer 

    } 
} 


// define an interface 
interface EnumTest {
     void Show (); 
} 


// custom enumeration class 
enum SeasonEnum the implements EnumTest { 
    
    // uppermost object class into the class enumeration and and custom simplified to:
    // second implementation Interface 
    SPRING ( "spring", "which is spring" ) {
         public  void Show () { 
            System.out.println ( "the Hi, spring" ); 
        } 
    },   
    WINTER ( "winter", "which winter " ) {
         public  void Show () { 
            System.out.println ( " the Hi, winter " ); 
        } 
    }, 
    sUMMER ( " summer "," this summer " ) {
         public  void Show () { 
            the System.out. the println ( "the Hi, summer" ); 
        } 
    },  
    AUTUMN ( "autumn", "This is the fall.") {
         Public  void Show () { 
            System.out.println ( "the Hi, fall" ); 
        }   
    }; 
    
    // provide attributes class declared as final Private 
    Private  final String seasonName;
     Private  final String seasonDesc; 
    
    
    // declared final property, in the initialization configuration 
    Private SeasonEnum (seasonName String, String seasonDesc) {
         the this .seasonName = seasonName;
         the this .seasonDesc = seasonDesc;     
    } 
    
    public String getSeasonName () {
         return seasonName; 
    } 

    publicGetSeasonDesc String () {
         return seasonDesc; 
    } 
            
    @Override 
    public String toString () {
         return "SeasonEnum [seasonName =" + seasonName + ", seasonDesc =" + seasonDesc + "]" ; 
    } 

    
/ *     a first implementation of the interface --- --- implementation of this is conventional 
    public void Show () { 
        System.out.println ( "which is" + this.getSeasonName ()); 
    } 
* /     
    
}

 

Guess you like

Origin www.cnblogs.com/sixfiv/p/11426204.html