java - TypeScript:向枚举添加更多数据

标签 java typescript enums

在 TypeScript 中,是否可以向枚举常量添加更多内容(属性、方法等),就像在 Java 中一样?

演示添加字段、方法和构造函数的 Java 示例:

public enum Planet {
    MERCURY (3.303e+23, 2.4397e6),
    VENUS   (4.869e+24, 6.0518e6),
    EARTH   (5.976e+24, 6.37814e6),
    MARS    (6.421e+23, 3.3972e6),
    JUPITER (1.9e+27,   7.1492e7),
    SATURN  (5.688e+26, 6.0268e7),
    URANUS  (8.686e+25, 2.5559e7),
    NEPTUNE (1.024e+26, 2.4746e7);

    private final double mass;   // in kilograms
    private final double radius; // in meters

    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
    }
    private double mass() { return mass; }
    private double radius() { return radius; }

    // universal gravitational constant  (m3 kg-1 s-2)
    public static final double G = 6.67300E-11;

    double surfaceGravity() {
        return G * mass / (radius * radius);
    }
    double surfaceWeight(double otherMass) {
        return otherMass * surfaceGravity();
    }
    // ...
}

最佳答案

不使用枚举,但您可以使用类和一些静态成员获得完全相同的东西:

class Planet {
    public static MERCURY = new Planet(3.303e+23, 2.4397e6);
    public static VENUS = new Planet(4.869e+24, 6.0518e6);
    public static EARTH = new Planet(5.976e+24, 6.37814e6);
    public static MARS = new Planet(6.421e+23, 3.3972e6);
    public static JUPITER = new Planet(1.9e+27, 7.1492e7);
    public static SATURN = new Planet(5.688e+26, 6.0268e7);
    public static URANUS = new Planet(8.686e+25, 2.5559e7);
    public static NEPTUNE = new Planet(1.024e+26, 2.4746e7);

    private mass: number;
    private radius: number;

    private constructor(mass: number, radius: number) {
        this.mass = mass;
        this.radius = radius;
    }

    public static G = 6.67300E-11;

    public surfaceGravity(): number {
        return Planet.G * this.mass / (this.radius * this.radius);
    }

    public surfaceWeight(otherMass: number) {
        return otherMass * this.surfaceGravity();
    }
}

console.log(Planet.MERCURY.surfaceGravity());

( code in playground )

在 java 中,为枚举中的每个项目创建一个静态实例,这意味着这确实做同样的事情,只是 java 有更好的语法来定义枚举。


编辑

这是一个具有等效 Planet.values() 的版本,java 将生成该版本:

class Planet {
    public static MERCURY = new Planet(3.303e+23, 2.4397e6);
    public static VENUS = new Planet(4.869e+24, 6.0518e6);
    ...

    private static VALUES: Planet[] = [];

    private mass: number;
    private radius: number;

    private constructor(mass: number, radius: number) {
        this.mass = mass;
        this.radius = radius;

        Planet.VALUES.push(this);
    }

    public static values() {
        return Planet.VALUES;
    }

    ...
}

第二次编辑

下面是实现 valueOf 的方法:

   public static valueOf(name: string): Planet | null {
        const names = Object.keys(this);
        for (let i = 0; i < names.length; i++) {
            if (this[names[i]] instanceof Planet && name.toLowerCase() === names[i].toLowerCase()) {
                return this[names[i]];
            }
        }

        return null;
    }

关于java - TypeScript:向枚举添加更多数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43376040/

相关文章:

跨不同 JVM 的 Java 关闭 Hook

c# - C# 中 ENUM 的命名约定通常都是大写吗?

coffeescript - 将 CoffeeScript 转换为 TypeScript

typescript - 如何禁用 ionic 输入?

angularjs - Typescript 和 AngularJS - 静态方法与服务

json - 尝试反序列化 json 中的枚举时索引值超出合法索引范围

c++ - 在 C++11 中,我可以引用在模板参数中定义的枚举类吗

java - 在 JTable 中选择行时索引错误

java - 套接字,java发送文件服务器客户端

java - 有没有办法简化 Java 模式匹配从 POST 请求中检索两条数据?