매 거 진 클래스: enum 키워드 로 매 거 진 클래스 를 정의 합 니 다.

8279 단어 enum
11public enum Grade{

                        A("A", "90-100"),B("B","80-89"),C("C", "70-79"),D("D", "80-89"),E("E", "90-100");

                        

                        private String tip;

                        private String desc;

                        

                        private Grade(String tip, String desc){

                            this.tip = tip;

                            this.desc = desc;

                        }

                    

                        public String getTip() {

                            return tip;

                        }

                    

                        public String getDesc() {

                            return desc;

                        }

                        

                    }

                2》       (            )

                    

                    eg:

                        public enum Season {//  :             abstract

                            SPRING{

                                @Override

                                public void show(){

                                    System.out.println("  ");

                                }

                            },SUMMER{

                                @Override

                                public void show() {

                                    System.out.println("  ");

                                }

                            },AUTOMN{

                                @Override

                                public void show() {

                                    System.out.println("  ");

                                }

                            },WINNTER{

                                @Override

                                public void show() {

                                    System.out.println("  ");

                                }

                            };

                            //    

                            public abstract void show();

                        }

            3)switch         

                eg:

                    Season season = Season.SPRING;

                    String msg = null;

                        switch(season){

                        case SPRING:msg = "1"; break;//    Season.SPRING

                        case SUMMER:msg = "2"; break;

                        case AUTOMN:msg = "3"; break;

                        case WINNTER:msg = "4"; break;

                        }

                    System.out.println(msg);        

            41》String name():        

                2》int ordinal():          , 0  

                3》type[] values():              ,      

                4》static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)://

                eg:

                    Season season = Season.SPRING;

                    System.out.println(season.name());//SPRING

                    System.out.println(season.ordinal());//0【  】

                    

                    Season[] seasons = season.values();//       

                    for(Season s : seasons){

                        System.out.println(s.name());

                    }

                    

                    //Season.valueOf(Season.class, "QQ");//   QQ         ,

                    //  IllegalArgumentException:  

                    

                    System.out.println("=="+Season.valueOf(Season.class, "SPRING").name());//  :SPRING.        

                    //Season.valueOf(Season.class, "spring");//      

                    

            5)  :

                1》           ,        

                2》         ,       ,        ,                     

                3》       switch   case  ,        ,        。

                4》           ,           。

            

        

          :             .Enum  Enumeration

 eration

좋은 웹페이지 즐겨찾기