【JAVA】浅谈java枚举类

一、什么情况下使用枚举类?

  有的时候一个类的对象是有限且固定的,这种情况下我们使用枚举类就比较方便?

二、为什么不用静态常量来替代枚举类呢?

    public static final int SEASON_SPRING = 1;
    public static final int SEASON_SUMMER = 2;
    public static final int SEASON_FALL = 3;
    public static final int SEASON_WINTER = 4;

  枚举类更加直观,类型安全。使用常量会有以下几个缺陷:

  1. 类型不安全。若一个方法中要求传入季节这个参数,用常量的话,形参就是int类型,开发者传入任意类型的int类型值就行,但是如果是枚举类型的话,就只能传入枚举类中包含的对象。

  2. 没有命名空间。开发者要在命名的时候以SEASON_开头,这样另外一个开发者再看这段代码的时候,才知道这四个常量分别代表季节。

三、枚举类入门

  先看一个简单的枚举类。

package enumcase;

public enum SeasonEnum {
    SPRING,SUMMER,FALL,WINTER;
}
  1. enum和class、interface的地位一样
  2. 使用enum定义的枚举类默认继承了java.lang.Enum,而不是继承Object类。枚举类可以实现一个或多个接口。
  3. 枚举类的所有实例都必须放在第一行展示,不需使用new 关键字,不需显式调用构造器。自动添加public static final修饰。
  4. 使用enum定义、非抽象的枚举类默认使用final修饰,不可以被继承。
  5. 枚举类的构造器只能是私有的。

四、枚举类介绍

  枚举类内也可以定义属性和方法,可是是静态的和非静态的。

package enumcase;

public enum SeasonEnum {
    SPRING("春天"),SUMMER("夏天"),FALL("秋天"),WINTER("冬天");

    private final String name;

    private SeasonEnum(String name)
    {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

    实际上在第一行写枚举类实例的时候,默认是调用了构造器的,所以此处需要传入参数,因为没有显式申明无参构造器,只能调用有参数的构造器。

  构造器需定义成私有的,这样就不能在别处申明此类的对象了。枚举类通常应该设计成不可变类,它的Field不应该被改变,这样会更安全,而且代码更加简洁。所以我们将Field用private final修饰。

五、枚举类实现接口

  枚举类可以实现一个或多个接口。与普通类一样,实现接口的时候需要实现接口中定义的所有方法,若没有完全实现,那这个枚举类就是抽象的,只是不需显式加上abstract修饰,系统化会默认加上。

  

package enumcase;

public enum Operation {
    PLUS{

        @Override
        public double eval(double x, double y) {
            return x + y;
        }

    },
    MINUS{

        @Override
        public double eval(double x, double y) {
            return x - y;
        }

    },
    TIMES{

        @Override
        public double eval(double x, double y) {
            return x * y;
        }

    },
    DIVIDE{

        @Override
        public double eval(double x, double y) {
            return x / y;
        }

    };

    /**
     * 抽象方法,由不同的枚举值提供不同的实现。
     * @param x
     * @param y
     * @return
     */
    public abstract double eval(double x, double y);

    public static void main(String[] args) {
        System.out.println(Operation.PLUS.eval(10, 2));
        System.out.println(Operation.MINUS.eval(10, 2));
        System.out.println(Operation.TIMES.eval(10, 2));
        System.out.println(Operation.DIVIDE.eval(10, 2));
    }
}

  Operatio类实际上是抽象的,不可以创建枚举值,所以此处在申明枚举值的时候,都实现了抽象方法,这其实是匿名内部类的实现,花括号部分是一个类体。我们可以看下编译以后的文件。

  

  共生成了五个class文件,这样就证明了PLUS,MINUS,TIMES,DIVIDE是Operation的匿名内部类的实例。

六、switch语句里的表达式可以是枚举值

  Java5新增了enum关键字,同时扩展了switch。

package enumcase;

public class SeasonTest {
    public void judge(SeasonEnum s)
    {
        switch(s)
        {
        case SPRING:
            System.out.println("春天适合踏青。");
            break;
        case SUMMER:
            System.out.println("夏天要去游泳啦。");
            break;
        case FALL:
            System.out.println("秋天一定要去旅游哦。");
            break;
        case WINTER:
            System.out.println("冬天要是下雪就好啦。");
            break;
        }
    }

    public static void main(String[] args) {
        SeasonEnum s = SeasonEnum.SPRING;
        SeasonTest test = new SeasonTest();
        test.judge(s);
    }
}

  case表达式中直接写入枚举值,不需加入枚举类作为限定。

本站部分内容来源于网络,如有侵犯您的权益,请通过以下方式联系我们:

邮箱:zytlsd@163.com

我们将在收到通知后第一时间处理。

网友留言(26条)

  1. 头像
    Brodie Hulsey 回复Ta 2 天前
    Attention: If this interests you,
    The $100 wealth solution.
    http://worldwidebytnex.com
  2. 头像
    Karen B. 回复Ta 4 天前
    Hi,

    Hope all is well.

    Long story short... I can link to your site 51keeplearning.com from 5x legit local business websites (>DR30) — all FOC, no money involved. In return, you’d link to 5 different of my client sites from your end for a mutual SEO boost.

    Interested? I can send you the site list to choose.

    Cheers,
    Karen
  3. 头像
    Ian Marlar 回复Ta 4 天前
    Reach millions of website owners fast. Targeted or random outreach—your choice. Visit contactformpromotion.com for details.
  4. 头像
    Nan Glasheen 回复Ta 4 天前
    Here is my site: http://sitesubmissionservice.top
  5. 头像
    William Whitelaw 回复Ta 11 天前
    Is guest posting an option? What topics do you accept? What’s your posting fee?
  6. 头像
    Venus Wasinger 回复Ta 14 天前
    Ever stop to think how much you're spending on marked-up CPMs for Connected TV ads?
    If you're using “premium” audience segments without verifying who you're actually reaching, that money is likely feeding someone else's family.
    No pressure. Here’s the site if you're open to see a revolutionary vision for digital advertising
    topshelfaudience.com using real-time Intent data from an Integration in our platform to Lotame.com. You can reach me at marketing@mrmarketingres.com or 843-720-7301. And if this isn't a fit please feel free to email me and I'll be sure not to reach out again.  Thanks!
  7. 头像
    Constance Plummer 回复Ta 17 天前
    If 51keeplearning requires support securing as high as $500K in working capital, I’d love to help. This is what we do best.

    Let me break it down: Every week, we support businesses like 51keeplearning get approved for $25K–$500K in business credit — even if they've been denied before or are not confident about approval.

    The greatest benefit? We make the process simple and quick.

    If you're:
    Interested in business growth,
    Restructure your finances,
    Put funds into R&D,
    Acquire machinery or tools…

    Then let’s talk.

    We’ve helped thousands of clients and maintain a 4.8-star rating on TrustPilot with hundreds of verified reviews.

    Just drop me a reply and let me know:
    That you're curious to learn more, or
    A time next week that works for a quick call.

    There’s nothing to lose — just expert insight and clear next steps to understand your funding potential.

    Talk soon,
    Constance
  8. 头像
    Karen B. 回复Ta 17 天前
    Hi,

    Hope all is well.

    Long story short... I can link to your site 51keeplearning.com from 5x legit local business websites (>DR30) — all FOC, no money involved. In return, you’d link to 5 different of my client sites from your end for a mutual SEO boost.

    Interested? I can send you the site list to choose.

    Cheers,
    Karen
  9. 头像
    Annmarie Slate 回复Ta 18 天前
    Reach millions of website owners fast. Targeted or random outreach—your choice. Visit contactformpromotion.com for details.
  10. 头像
    Almeda Foxall 回复Ta 18 天前
    Here is my site: https://submissiontodirectory.top/

发表评论