首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 枚举

Java 枚举

作者头像
Mirror王宇阳
发布2020-11-10 23:06:19
1.1K0
发布2020-11-10 23:06:19
举报

金言:少用则用之!

枚举:

【多例设计】

package cn.mldn.utli;

class Color {
    private String title ;
    //多例设计模式
    private static final Color RED = new Color("红色");
    private static final Color GRE = 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 GRE ;
        case 3 : 
            return BLUE ;
        default :
            return null ;
        }
    }
    public String toString() {
        return this.title ;
    }
}

public class TestDemo {

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

枚举严格来讲所有语言都支持!在枚举产生之前,都是采用多例设计模式实现。

枚举概念产生后,出现了新的 enum 关键字:

定义枚举:

package cn.mldn.utli;

enum Color {  //定义枚举类
    RED,GRE,BLUE;   // 表示此处为实例化对象
    
}

public class TestDemo {

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

定义枚举之后,和先前的多例设计code相比较,枚举可以完全简化的替代多例设计模式

枚举关键字 enum定义的枚举类实际上就是继承了 Enum 类的子类:

  Enum是一个抽象类;Enum类中定义了两个方法:

    |-- 取得枚举的索引: public final int ordinal()

    |-- 取得枚举的名字: public final String name()

    |-- 枚举类之中还有一个 values()方法,它将对象以对象数组的形式返回。

___________________________________________

定义其他结构:

  枚举之中定义的构造法方法,不可以使用public声明,必须构造私有化;

  枚举对象必须放在首行,其后才会定义属性、构造、方法;

enum Color {
    RED("红色"),BLU("绿色"),GREEN("黄色");  //枚举对象定义在枚举类中的首行
    private String title ; // 枚举类中的属性
    private Color(String title) {
        this.title = title ;
    } 
    public String toString() {
        return this.title;
    }

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

枚举实现接口:

interface Message{  // 定义接口类
    public String getTitle() ;
}

enum Color implements Message {  // Color枚举类实现接口
    RED("红色"),BLU("绿色"),GREEN("黄色");  //枚举对象定义在枚举类中的首行
    private String title ; // 枚举类中的属性
    private Color(String title) {
        this.title = title ;
    } 
    public String toString() {
        return this.title;
    }
    public String getTitle() { 
        // 覆写接口类方法
        return this.title;
    }

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

——————————

枚举的应用:

枚举中支持swicth语句函数 >>>>

enum Color {
    RED , GREEN , BLUE ;
}
public class TestDemo {
    public static void main(String [] args) {
        Color c = Color.RED;
        switch(c) {
            case RED : 
                System.out.println("RED") ;
                break ;
            case GREEN :
                System.out.println("GREEN") ; 
                break ;
            case BLUE :
                System.out.println("BLUE") ;
                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 TestDemo {
    public static void main(String [] args) {
        System.out.println(new Person("于哥" , 24 , Sex.MALE));    
    }
}

————————

总结:

  枚举属于高级的多例设计模式

  枚举的使用根据个人是否习惯使用,不习惯使用的可以依旧使用多例设计模式

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档