Java 枚举和具有私有构造函数的类有什么区别? [英] What are the differences between a Java enum and a class with private constructor?

查看:26
本文介绍了Java 枚举和具有私有构造函数的类有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 Java 枚举的真正工作原理,我得出的结论是,它与构造函数声明为私有的普通 Java 类非常相似.

I was trying to understand how Java enum really works and I have come to the conclusion that it is very similar to a normal Java class that has its constructor declared private.

我刚刚得出这个结论,并没有经过太多思考,但我想知道我是否遗漏了什么.

I have just come to this conclusion and it isn't based on much thinking, but Id like to know whether I miss anything.

下面是一个简单的 Java 枚举和一个等效的 Java 类的实现.

So below is an implementation of a simple Java enum and an equivalent Java class.

public enum Direction {
    ENUM_UP(0, -1),
    ENUM_DOWN(0, 1),
    ENUM_RIGHT(1, 0),
    ENUM_LEFT(-1, 0);


    private int x;
    private int y;

    private Direction(int x, int y){
        this.x = x;
        this.y = y;
    }
    public int getEnumX(){
        return x;
    }
    public int getEnumY(){
        return y;
    }
}

上面和下面的代码在含义上有什么区别?

What is the difference in meaning between the code above and below?

public class Direction{
    public static final Direction UP = new Direction(0, -1) ;
    public static final Direction DOWN = new Direction(0, 1) ;
    public static final Direction LEFT = new Direction(-1, 0) ;
    public static final Direction RIGHT = new Direction(1, 0) ;


    private int x ;
    private int y ;

    private Direction(int x, int y){
        this.x = x ;
        this.y = y ;
    }
    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }
}

推荐答案

区别:

  1. 枚举扩展了 java.lang.Enum 并获得了它的所有 不错的功能:
  1. Enums extend java.lang.Enum and gain all of its nice features:
  1. 通过正确的序列化实现自动单例行为
  2. 自动对枚举值进行可读的 .toString 方法,无需复制枚举名称
  3. .name.ordinal 特殊目的方法
  4. 可用于基于位集的高性能EnumSetEnumMap
  1. Automatic singleton behaviour through correct serialization
  2. Automatic human-readable .toString method on enum values without the need to duplicate your enum names
  3. .name and .ordinal special-purpose methods
  4. Usable in high-performance bitset-based EnumSet and EnumMap classes

  • 枚举由语言特殊处理:

  • Enums are treated by the language specially:

    1. 枚举使用特殊的语法,无需编写数十个public static final字段即可简化实例的创建
    2. 枚举可用于switch 语句
    3. 枚举不能在枚举列表之外实例化,除非使用反射
    4. 枚举不能扩展到枚举列表之外
    1. Enums use a special syntax which simplifies instance creation without writing dozens of public static final fields
    2. Enums can be used in switch statements
    3. Enums cannot be instantiated outside the enumeration list except by using reflection
    4. Enums cannot be extended outside the enumeration list

  • Java 会自动将额外的内容编译为枚举:

  • Java automatically compiles extra stuff into enums:

    1. public static (Enum)[] values();
    2. public static (Enum) valueOf(java.lang.String);
    3. private static final (Enum)[] $VALUES;(values() 返回一个副本)
    1. public static (Enum)[] values();
    2. public static (Enum) valueOf(java.lang.String);
    3. private static final (Enum)[] $VALUES; (values() returns a clone of this)

  • 其中大部分都可以通过适当设计的类来模拟,但是 Enum 只是让创建具有这组特别理想属性的类变得非常容易.

    Most of these can be emulated with a suitably designed class, but Enum just makes it really easy to create a class with this set of particularly desirable properties.

    这篇关于Java 枚举和具有私有构造函数的类有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆