在使用java多年後我正在嘗試進入scala。 可以說我有一個簡單枚舉這樣在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;
}
}
與實況和計算器我得到儘可能一些幫助:
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) : String = f match {
case Example => "test"
case Another => "other"
}
}
但我不喜歡這樣的幾個原因。 首先,值遍佈各種方法,每次添加新條目時都需要更改它們。 其次,如果我想調用一個函數,它將以Foo.getSomething(Another)的形式或類似的東西,我覺得很奇怪,我寧願另一個.getSomething。 我希望將這些改爲更優雅的建議。
[scala:add m方法到枚舉](http://stackoverflow.com/questions/12345823/scala-add-methods-to-an-enum) –