Java--枚举

枚举

枚举简介

1、多例设计模式:
构造方法私有化,而后在类的内部提供有若干个实例化对象,并且通过static方法返回。
范例:定义一个表示颜色基色的多例

class Color {
    private String title;
    private static final Color RED = new Color("红色");
    private static final Color GREEN = new Color("绿色");
    private static final Color BLUE = new Color("蓝色");

    private Color(String title) {
        this.title = title;
    }

    public static Color getInstance(int ch) {
        switch (ch) {
            case 1:
                return RED;
            case 2:
                return GREEN;
            case 3:
                return BLUE;
            default:
                return null;
        }
    }

    public String toString() {
        return this.title;
    }
}

public class Demo {
    public static void main(String[] args) {
        Color red = Color.getInstance(1);
        System.out.println(red);
    }
}

2005年之前,Java定义枚举采用上述方式,即多例设计解决了Java无法直接定义枚举的问题。
2、从2005年之后,Java增加了枚举的概念,使用enum关键字定义。
范例:定义枚举

enum Color { // 定义枚举类
    RED, GREEN, BLUE; // 实例化对象
}

public class Demo {
    public static void main(String[] args) {
        Color red = Color.RED;
        System.out.println(red);
    }
}

枚举可以地替代多例设计模式。
3、Java使用enum定义枚举,相当于一个类继承了Enum类。

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

Enum是一个抽象类,里面定义的构造方法如下:protected Enum(String name, int ordinal)
Enum类的构造方法依然是被封装的,也属于构造方法私有化。多例设计模式的前提:构造方法私有化。
4、在Enum类中定义了两个方法:

· 取得枚举的索引:public final int ordinal();
· 取得枚举的名字:public final String name();
除了以上支持的方法外,使用enum关键字定义的枚举类还有一个values()方法,可以将枚举对象以对象数组的形式的返回。

enum Color { // 定义枚举类
    RED, GREEN, BLUE;
}

public class Demo {
    public static void main(String[] args) {
        for (Color c : Color.values()) {
            System.out.println(c.ordinal() + "-" + c.name());
        }
    }
}

请解释enum和Enum的区别?
· enum是一个关键字,而Enum是一个抽象类;
· 使用enum定义的枚举就相当于一个类继承了Enum类。

定义其它结构

1、多例设计模式可以在类中定义属性和方法等;枚举也可以,但有如下要求:

· 枚举之中定义的构造方法不能使用public声明,如果没有无参构造方法,要手工调用构造传递参数;
· 枚举对象必须要放在首行,随后才可以定义属性、构造方法、普通方法。

范例:扩充枚举功能

enum Color { // 定义枚举类
    RED("红色"), GREEN("绿色"), BLUE("蓝色"); // 对象必须要放在首行
    private String title; //属性

    private Color(String title) {
        this.title = title;
    }

    public String toString() {
        return this.title;
    }
}

public class Demo {
    public static void main(String[] args) {
        for (Color c : Color.values()) {
            System.out.println(c); // 调用toString()
        }
    }
}

此时与之前定义的多例设计模式操作方式完全相同,而且代码更加简单。
2. 枚举还可以实现接口
范例:枚举实现接口

interface Message {
    public String getTitle();
}

enum Color implements Message { // 定义枚举类
    RED("红色"), GREEN("绿色"), BLUE("蓝色"); // 对象必须要放在首行
    private String title; //属性

    private Color(String title) {
        this.title = title;
    }

    public String getTitle() {
        return this.title;
    }

    public String toString() {
        return this.title;
    }
}

public class Demo {
    public static void main(String[] args) {
        Message msg = Color.RED;
        System.out.println(msg.getTitle());
    }
}

3、枚举还可以在每个对象后面以匿名内部类的形式使用抽象方法。
范例:另一种形式的接口

interface Message {
    public String getTitle();
}

enum Color implements Message { // 定义枚举类
    RED("红色") {
        public String getTitle() {
            return "自己的" + this;
        }
    }, GREEN("绿色") {
        public String getTitle() {
            return "自己的" + this;
        }
    }, BLUE("蓝色") {
        public String getTitle() {
            return "自己的" + this;
        }
    }; // 对象必须要放在首行
    private String title; //属性

    private Color(String title) {
        this.title = title;
    }

    public String getTitle() {
        return this.title;
    }

    public String toString() {
        return this.title;
    }
}

public class Demo {
    public static void main(String[] args) {
        Message msg = Color.RED;
        System.out.println(msg.getTitle());
    }
}

4、枚举中还能直接定义抽象方法,此时每一个枚举对象必须分别覆写抽象方法。
范例:定义抽象方法并覆写

enum Color { // 定义枚举类
    RED("红色") {
        public String getTitle() {
            return "自己的" + this;
        }
    }, GREEN("绿色") {
        public String getTitle() {
            return "自己的" + this;
        }
    }, BLUE("蓝色") {
        public String getTitle() {
            return "自己的" + this;
        }
    }; // 对象必须要放在首行
    private String title; //属性

    private Color(String title) {
        this.title = title;
    }

    public String toString() {
        return this.title;
    }

    public abstract String getTitle();
}

public class Demo {
    public static void main(String[] args) {
        System.out.println(Color.RED.getTitle());
    }
}

枚举应用

1、枚举应用于switch。

public class Demo {
    public static void main(String[] args) {
        Color c =Color.RED;
        switch (c){ // 枚举判断
            case RED:
                System.out.println("这是红色");
                break;
            case GREEN:
                System.out.println("这是绿色");
                break;
            case BULE:
                System.out.println("这是蓝色");
                break;
        }
    }
}

范例:利用枚举编写一个程序:

enum Sex {
    MALE("男"), FEMALE("女");
    private String title;

    private Sex(String title){
        this.title = title;
    }
    public String toString(){
        return this.title;
    }
}

class Person{
    private String name;
    private int age;
    private Sex sex;
    public Person(String name,int age,Sex sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public String toString(){
        return "姓名:" + this.name +
                ",年龄:" + this.age +
                ",性别:" + this.sex;
    }
}
public class Demo {
    public static void main(String[] args) {
        System.out.println(new Person("张三", 24, Sex.FEMALE));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值