有没有办法在Java中创建字符串字面类型?

huangapple go评论65阅读模式
英文:

Is the any way to create String Literal Types in Java?

问题

我对Java还不熟悉。

我一直在前端使用TypeScript为项目进行类型设置,我非常习惯于创建字符串字面类型来限制可能的输入。在Java中有没有简单的方法可以实现这一点?

为了美观起见,以下是在TypeScript中创建它的方式:

type Easing = "ease-in" | "ease-out" | "ease-in-out";
英文:

I'm new to Java.

I've been using Typescript to type my projects in the Front-end and I'm very used to creating String Literal Types to limit possible inputs. Is there any easy way to this in Java?

For eye candy, here's how you create it in Typescript:<br>

type Easing = &quot;ease-in&quot; | &quot;ease-out&quot; | &quot;ease-in-out&quot;;

答案1

得分: 1

最相似的东西是一个 enum 类型

public enum Easing {
    EASE_IN, EASE_OUT, EASE_IN_OUT;
}
英文:

No. The most similar thing is an enum Type.

public enum Easing {
    EASE_IN, EASE_OUT, EASE_IN_OUT;
}

huangapple
  • 本文由 发表于 2020年9月24日 09:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64038295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定