在Java中你可以:
public enum Enum {
ONE {
public String method() {
return "1";
}
},
TWO {
public String method() {
return "2";
}
},
THREE {
public String method() {
return "3";
}
};
public abstract String method();
}
Run Code Online (Sandbox Code Playgroud)
你是如何在Scala中做到这一点的?
编辑/有用的链接:
使用java多年后,我试图进入scala.可以说我有一个像这样简单的枚举
public enum Foo{
Example("test", false),
Another("other", true);
private String s;
private boolean b;
private Foo(String s, boolean b){
this.s = s;
this.b = b;
}
public String getSomething(){
return this.s;
}
public boolean isSomething(){
return this.b;
}
}
Run Code Online (Sandbox Code Playgroud)
随着文档和stackoverflow的一些帮助,我得到了:
object Foo extends Enumeration
{
type Foo = Value
val Example, Another = Value
def isSomething( f : Foo) : Boolean = f match {
case Example => false
case Another => true
}
def getSomething( f : Foo) : …Run Code Online (Sandbox Code Playgroud)