枚举案例;Enum

枚举案例:
        package com.eron;

        public class Test {
            public static void main(String[] args) {
                Sex sex = Sex.女;
                switch (sex){
                    case 女:
                        System.out.println("我是一个女生");
                        break;
                    case 男:
                        System.out.println("我是一个男生");
                        break;
                }
                }

            }

            package com.eron;

            public enum Sex {
                男,
                女;
            }
** JDK1.5之前**
        package com.eron;
        public class emor {
            private final String SEASONName;
            private final String SEASONType;
        //利用构造器给属性赋值;成员变量可以不用赋值;但final修饰的变量必须先赋值;再使用
            private emor(String seasonName, String seasonType) {
                this.SEASONName = seasonName;
                this.SEASONType = seasonType;
            }
            @Override
            public String toString() {
                return "emor{" +
                        "SEASONName='" + SEASONName + '\'' +
                        ", SEASONType='" + SEASONType + '\'' +
                        '}';
            }
            public String getSEASONName() {
                return SEASONName;
            }

            public String getSEASONType() {
                return SEASONType;
            }
            //这里是通过枚举;做的对象;(有限的,确定的)
            public static final emor SPRING = new emor("春天","万物复苏");
            public static final emor SUMMER = new emor("夏天","赤日炎炎");
            public static final emor AUTUMN = new emor("秋天","硕果累累");
            public static final emor WINNER = new emor("冬天","银装素裹");
            //调用:
            public static void main(String[] args) {
                emor autumn = emor.AUTUMN;
                System.out.println(autumn);
            }
        }

枚举调用方法:
        package com.eron;
//测试
        public class Test {
            public static void main(String[] args) {
                Sex.女.show();
                }

            }
//枚举
        package com.eron;
        public enum Sex implements TestInterface{
            男{
                @Override
                public void show() {
                    System.out.println("这是男....");
                }
            },
            女{
                @Override
                public void show() {
                    System.out.println("这是女....");
                }
            };

        }

posted @ 2022-04-23 16:44  爱豆技术部  阅读(24)  评论(0)    收藏  举报
TOP