相关疑难解决方法(0)

如何在Scala中向Enumeration添加方法?

在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 enums enumeration scala

38
推荐指数
7
解决办法
1万
查看次数

在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)

enums scala

2
推荐指数
1
解决办法
1968
查看次数

标签 统计

enums ×2

scala ×2

enumeration ×1

java ×1