java enum的常见用法

enum 用法

1.enum基本用法一

public enum Season {
    WINTER,
    SPRING,
    SUMMER,
    FALL
}

每一个枚举常量都是public static final的,所以我们可以直接通过枚举名直接访问.values()会转成对应的数组.并且enum隐式的实现了实现Serializable和Comparable.

public abstract class Enum<E extends Enum<E>>
    extends Object
    implements Comparable<E>, Serializable

2.enum基本用法二

    public enum Coin {
        PENNY(1), NICKEL(5), DIME(10), QUARTER(25); 

        private int value;

        // 构造方法与括号内的参数要匹配
        Coin(int value) { 
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    int p = Coin.NICKEL.getValue();

转化为对应的class类为:

    public class Coin<T extends Coin<T>> implements Comparable<T>, Serializable{
        public static final Coin PENNY = new Coin(1);
        public static final Coin NICKEL = new Coin(5);
        public static final Coin DIME = new Coin(10);
        public static final Coin QUARTER = new Coin(25);

        private int value;

        private Coin(int value){
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    int p = Coin.NICKEL.getValue(); // 5

但是,最佳实践应该是保证value是不可变的,所以应该是 private fianl int value;

3.其他用法

enum还可以定义一个抽象方法,每个常量都需要实现该方法.

enum Action {
    DODGE {
        public boolean execute(Player player) {
            return player.isAttacking();
        }
    },
    ATTACK {
        public boolean execute(Player player) {
            return player.hasWeapon();
        }
    },
    JUMP {
        public boolean execute(Player player) {
            return player.getCoordinates().equals(new Coordinates(0, 0));
        }
    };

    public abstract boolean execute(Player player);
}

enum实现接口:

import java.util.function.Predicate;
import java.util.regex.Pattern;

enum RegEx implements Predicate<String> {
    UPPER("[A-Z]+"), LOWER("[a-z]+"), NUMERIC("[+-]?[0-9]+");

    private final Pattern pattern;

    private RegEx(final String pattern) {
        this.pattern = Pattern.compile(pattern);
    }

    @Override 
    public boolean test(final String input) {
        return this.pattern.matcher(input).matches();
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(RegEx.UPPER.test("ABC"));
        System.out.println(RegEx.LOWER.test("abc"));
        System.out.println(RegEx.NUMERIC.test("+111"));
    }
}

4.enum比较

public static void enumSwitchExample(Season s) {
    switch(s) {
        case WINTER:
            System.out.println("It's pretty cold");
            break;
        case SPRING:
            System.out.println("It's warming up");
            break;
        case SUMMER:
            System.out.println("It's pretty hot");
            break;
        case FALL:
            System.out.println("It's cooling down");
            break;
    }
}

可以通过==进行比较:

Season.FALL == Season.WINTER    // false
Season.SPRING == Season.SPRING  // true

还可以通过equals()进行比较,不过需要避免一些坑:

Season.FALL.equals(Season.FALL); // true
Season.FALL.equals(Season.WINTER); // false
Season.FALL.equals("FALL"); // false and no compiler error
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值