Java枚举enum

不羁的心 提交于 2020-01-08 09:23:31

1.普通枚举

public class EnumTest01 {
    /**
     *普通枚举
     */
    public enum Fruit {
        APPLE, BANANA, PEAR, CHERRY;
    }
    public static void main(String[] args) {
        //遍历
        for (Fruit f : Fruit.values()) {
            System.out.println(f);
        }
        Fruit f = Fruit.APPLE;
        //结合switch
        switch (f) {
        case APPLE:
            System.out.println("APPLE");
            break;
        case BANANA:
            System.out.println("BANANA");
            break;
        case PEAR:
            System.out.println("PEAR");
            break;
        case CHERRY:
            System.out.println("CHERRY");
            break;

        default:
            System.out.println("UNKNOW FRUIT");
            break;
        }
    }
}
View Code

2.带构造方法,普通方法,静态方法,成员变量,重写toString()方法

package com.jte.enum1;

public class EnumTest01 {
    /**
     * 带构造方法,普通方法,静态方法,成员变量,重写toString()方法
     */
    public enum Fruit {
        APPLE("苹果", 1), BANANA("香蕉", 2), PEAR("梨", 3), CHERRY("樱桃", 4);
        // 成员变量
        private String name;
        private int index;

        // 构造方法
        Fruit(String name, int index) {
            this.name = name;
            this.index = index;
        }

        // 普通静态方法
        public static String getFruitName(int index) {
            for (Fruit f : Fruit.values()) {
                if (f.getIndex() == index) {
                    return f.getName();
                }
            }
            return "未找到相关水果";
        }

        // 普通方法
        public String getInfo2(int... index) {
            StringBuilder sb = new StringBuilder();
            for (int i : index) {
                sb.append(Fruit.getFruitName(i)).append("、");
            }
            return sb.toString().substring(0, sb.length() - 1);
        }

        // setter and getter
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }
        //重写toString()方法
        public String toString() {
            return "Name: " + this.name + "Index:" + this.index;
        }
    }

    public static void main(String[] args) {
        Fruit f = Fruit.APPLE;
        System.out.println(f.getIndex());
        System.out.println(f.getInfo2(1, 3, 2, 4, 5));
        System.out.println(Fruit.getFruitName(1));
        System.out.println(f);//输出重写toString()方法后的结果
    }
}
View Code

 3.结合接口使用

package com.jte.enum1;

public class EnumTest02 {
    public interface Behaviour{
        String getName();
        int getIndex();
        String getInfo();
    }
    public enum Fruit1 implements Behaviour{
        APPLE("苹果", 1), BANANA("香蕉", 2), PEAR("梨", 3);
        public String name;
        public int index;
        Fruit1(String name, int index){
            this.name = name;
            this.index = index;
        }
        @Override
        public String getName() {
            // TODO Auto-generated method stub
            return this.name;
        }
        
        @Override
        public int getIndex() {
            // TODO Auto-generated method stub
            return this.index;
        }
        
        @Override
        public String getInfo() {
            // TODO Auto-generated method stub
            return "Name: " + this.name + "Index:" + this.index;
        }
    }
    public static void main(String[] args) {
        Fruit1 f = Fruit1.APPLE;
        System.out.println(f.getInfo());
    }
    
}
View Code

 4.结合接口使用2

package com.jte.enum1;


public class EnumTest03 {
    interface Food{
        public enum Vegetables {
            PUMPKIN, CHILI, POTATO;
        }
        public enum Fruit{
            APPLE, BANANA, PEAR;
        }
        public enum Drink{
            WATER, WINE, JUICE;
        }
    }
    public static void main(String[] args) {
        Food.Fruit f = Food.Fruit.APPLE;
        System.out.println(f);
    }
}
View Code

 5.遍历枚举

package com.jte.enum1;

import java.util.EnumSet;

public class EnumTest04 {
    public enum Animal{
        DOG, CHICKEN, DARK, DRAGON;
    }
    public static void main(String[] args) {
        //1
        System.out.println("==================1====================");
        for (int i = 0; i < Animal.values().length; i++) {
            System.out.println(Animal.values()[i]);;
        }
        //2
        System.out.println("==================2====================");
        for (Animal animal : Animal.values()) {
            System.out.println(animal);
        }
        //3
        System.out.println("==================3====================");
        EnumSet<Animal> enumSet = EnumSet.allOf(Animal.class);
        for (Animal animal : enumSet) {
            System.out.println(animal);
        }
    }
}
View Code

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!