• enum枚举类


    枚举类可用于定义常量
    ch01
    package edu.nf.demo.ch01;
    
    /**
     *
     * 枚举类型
     */
    public enum Color {
    
        /**
         * 红色
         */
        RED,
        /**
         * 黄色
         */
        YELLOW,
        /**
         * 蓝色
         */
        BLUE,
        /**
         * 绿色
         */
        GREEN,
        /**
         * 黑色
         */
        BLACK
    
    }
    Color

     ch01

    public class Main {
    
        public static void main(String[] args) {
            //test1();
            test2(Color.RED);
            //test3(Color.RED);
        }
    
        private static void test1(){
            //Color.RED得到的是一个Color枚举元素的实例
            System.out.println("枚举实例:"+Color.RED);
            System.out.println("枚举元素名称:"+Color.RED.name());
            System.out.println("枚举元素下标:"+Color.BLUE.ordinal());
            System.out.println(Color.BLUE);
            System.out.println(Color.BLUE.name());
            System.out.println(Color.BLUE.ordinal());
            System.out.println("---------------");
            for (Color value : Color.values()) {
                System.out.println(value);
            }
        }
    
        private static void test2(Color color){
            System.out.println(color);
            switch (color) {
                case RED:
                    System.out.println("This is red color");
                    break;
                case BLUE:
                    System.out.println("This is blue color");
                    break;
                default:
                    System.out.println("no color");
            }
        }
    
        private static void test3(Color color){
            System.out.println(color);
            if(color.equals(Color.RED)){
                System.out.println("This is red color");
            }else if(color.equals(Color.BLUE)){
                System.out.println("This is blue color");
            }else{
                System.out.println("no color");
            }
        }
    }
    main
    ch02
    public enum Color {
    
        /**
         * 红色
         */
        RED(0),
        /**
         * 黄色
         */
        YELLOW(1),
        /**
         * 蓝色
         */
        BLUE(2),
        /**
         * 绿色
         */
        GREEN(3),
        /**
         * 黑色
         */
        BLACK(4)
        ; //分隔符
    
        //声明一个实例变量
        private Integer color;
    
        //构造方法
        Color(Integer color){
            this.color = color;
        }
    
        //提供一个get方法用于获取color变量的值
        public Integer getColor() {
            return color;
        }
    }
    Color(枚举自定义构造函数以及添加方法)

    ch02

    public class Main {
    
        public static void main(String[] args) {
            System.out.println(Color.YELLOW.name());
            System.out.println(Color.YELLOW.getColor());
        }
    }
    main

    ch03

    public enum Color {
    
    
        /**
         * 红色
         */
        RED{
            @Override
            public void printColor() {
                System.out.println("This is red.");
            }
        },
        /**
         * 蓝色
         */
        BLUE{
            @Override
            public void printColor() {
                System.out.println("This is blue");
            }
        };
    
        /**
         * 在枚举中声明一个抽象方法,
         * 并且在每一个枚举元素中是使用匿名内部类的方式进行实现
         */
        public abstract void printColor();
    }
    Color枚举中定义抽象方法,类似于继承重载里面的方法

    ch03

    public class Main {
    
        public static void main(String[] args) {
            Color.RED.printColor();
        }
    }
    main

    ch04

    public enum Color implements PrintInf {
    
        /**
         * 红色
         */
        RED{
            @Override
            public void printColor() {
                System.out.println("This is red.");
            }
        },
        /**
         * 蓝色
         */
        BLUE{
            @Override
            public void printColor() {
                System.out.println("This is blue.");
            }
        }
    }
    
    public enum Color2 implements PrintInf {
    
        /**
         * 红色
         */
        RED("red"),
        /**
         * 蓝色
         */
        BLUE("blue")
        ;
        private String color;
    
        Color2(String color){
            this.color = color;
        }
    
        @Override
        public void printColor() {
            System.out.println("Print color: " + color);
        }
    }
    Color继承接口

    ch04

    public interface PrintInf {
    
        void printColor();
    }
    
    public class Main {
    
        public static void main(String[] args) {
            PrintInf pf = Color.RED;
            pf.printColor();
    
            Color2.RED.printColor();
        }
    }
    接口和main

    ch05

    public interface Food {
        /**
         * 开胃菜
         */
        enum Appetizer implements Food {
            /**
             * 沙拉
             */
            SALAD("沙拉"),
            /**
             * 汤
             */
            SOUP("汤")
            ;
            private String foodName;
    
            Appetizer(String foodName){
                this.foodName = foodName;
            }
    
            public String getFoodName() {
                return foodName;
            }
        }
    
        /**
         * 主食
         */
        enum MainCourse implements Food {
            /**
             * 千层面
             */
            LASAGNE("千层面"),
            /**
             * 卷饼
             */
            BURRITO("卷饼")
            ;
    
            private String foodName;
    
            MainCourse(String foodName){
                this.foodName = foodName;
            }
    
            public String getFoodName() {
                return foodName;
            }
        }
    
    }
    
    public class Main {
    
        public static void main(String[] args) {
            System.out.println(Food.Appetizer.SALAD.getFoodName());
        }
    }
    Color在接口中声明枚举类型
     
  • 相关阅读:
    Shell基础
    不错的设计类网站
    win7旗舰版 OEM KEY
    js获取url参数值
    在ASP.Net中利用JS调用Aspx页面的输出
    Virtual Router – 为易用而生的虚拟WiFi热点 (虚拟路由器)
    php5.3.8安装体验
    WIN2003+IIS6+PHP5.3.8配置
    PHP环境一键安装包 ZkeysPHP
    诡异的apache RewriteCond %{REQUEST_FILENAME} !s问题
  • 原文地址:https://www.cnblogs.com/ssjf/p/10483253.html
Copyright © 2020-2023  润新知