Scala 中的枚举具有多个构造函数参数
我正在编写我的第一个大型 Scala 程序。在 Java 中,我有一个枚举,其中包含 UI 控件的标签和工具提示:
public enum ControlText {
CANCEL_BUTTON("Cancel", "Cancel the changes and dismiss the dialog"),
OK_BUTTON("OK", "Save the changes and dismiss the dialog"),
// ...
;
private final String controlText;
private final String toolTipText;
ControlText(String controlText, String toolTipText) {
this.controlText = controlText;
this.toolTipText = toolTipText;
}
public String getControlText() { return controlText; }
public String getToolTipText() { return toolTipText; }
}
不用介意为此使用枚举的智慧。我想在其他地方做类似的事情。
如何在 Scala 中使用 scala.Enumeration 执行此操作? Enumeration.Value 类仅采用一个 String 作为参数。我需要将其子类化吗?
谢谢。
I am writing my first large Scala program. In the Java equivalent, I have an enum that contains labels and tooltips for my UI controls:
public enum ControlText {
CANCEL_BUTTON("Cancel", "Cancel the changes and dismiss the dialog"),
OK_BUTTON("OK", "Save the changes and dismiss the dialog"),
// ...
;
private final String controlText;
private final String toolTipText;
ControlText(String controlText, String toolTipText) {
this.controlText = controlText;
this.toolTipText = toolTipText;
}
public String getControlText() { return controlText; }
public String getToolTipText() { return toolTipText; }
}
Never mind the wisdom of using enums for this. There are other places that I want to do similar things.
How can I do this in Scala using scala.Enumeration? The Enumeration.Value class takes only one String as a parameter. Do I need to subclass it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样做,这与枚举的使用方式相匹配:
You could do this which matches how enums are used:
我想针对该问题提出以下解决方法:
现在您可以使用
ControlText
作为 Java 枚举:唯一的一点不好的味道是通过
withName
获取枚举对象或apply
方法。你必须进行演员表转换:I'd like to propose the following workaround for the issue:
Now you can use
ControlText
as Java enum:The only a little bad smell is to get enum object by
withName
orapply
methods. You have to do a cast:根据 Mitch 的回答,如果您发现密封行为在将子类实例限制到定义基类的文件方面限制不够,您可以使用如下所示的对象(模块)定义:
这显然限制了 ControlText 的进一步实例化实例。 Sealed 关键字对于帮助检测模式匹配中的缺失情况仍然很重要。
Following on from Mitch's answer, if you find that the sealed behaviour is not restrictive enough in limiting subclassed instances to the file where the base class is defined, you can use an object (module) definition like this:
which obviously limits further instantiation of ControlText instances. The sealed keyword is still important in helping detect missing cases in pattern matching.