java高级开发-枚举

enum 关键字 定义枚举类
Enum 枚举类的父类

枚举类:
枚举常量
静态方法valueOf values()
定义结构:静态方法和属性,成员方法和属性,实现接口,覆写Enum类的方法
应用场景:一个类的实例化对象可数的(性别,状态,周期,颜色)
支持swith

public enum Color implements IColor {

    //0  1      2
    RED("红色", "#ff0000"),
    GREEN("绿色", "#0ff00"),
    BLACK("黑色", "#00000"),
    BLUE("蓝色", "#0000ff");//枚举常量

    private String title;
    private final String rgb;
    private static String MESSAGE = "Hello";

    Color(String title, String rgb) {
        this.title = title;
        this.rgb = rgb;
    }


    public static void main(String[] args) {
        //1. 输出所有的枚举常量
        /*
        Color[] colors = Color.values();
        for (Color color : colors) {
            System.out.println(color);
        }
        */
        //2. 通过枚举常量的名字(String) 获取枚举常量对象
        /*
        Color color = Color.valueOf("RED");
        System.out.println(color);
        System.out.println(Color.valueOf("red"));
         */
        /*
       Color color= Enum.valueOf(Color.class, "BLUE");
        System.out.println(color);
         */
//        for (Color color : Color.values()){
//            System.out.println(color);//toString()
//        }

        //int char short
        //enum
        //String

//        Color color = Color.RED;
//        switch (color) {
//            case RED:
//                System.out.println("中国色");
//                break;
//            case BLUE:
//                System.out.println("澳大利亚");
//            case BLACK:
//                System.out.println("");
//            case GREEN:
//                System.out.println();
//            default:
//        }

        //JDK7之前不支持switch JDK7支持switch
        //不可变,unicode
        //"一" "\u4e00"
        String message = "二";
        switch (message) {
            case "一": {
                System.out.println("OK");
                break;
            }
            default:{
                System.out.println("default");
            }
        }
    }

    public String getTitle() {
        return title;
    }

    @Override
    public String toString() {
        return this.name() + " " + this.title + " rgb(" + this.rgb() + ")";
    }

    //成员方法
    public void print() {
        System.out.println(this);
    }

    @Override
    public String rgb() {
        return this.rgb;
    }
}

interface IColor {
    //#ffffff
    String rgb();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值