如何在Scala中添加枚举方法? [英] How to add a method to Enumeration in Scala?
本文介绍了如何在Scala中添加枚举方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中,您可以:
In Java you could:
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();
}
您如何在Scala中执行此操作?
How do you do this in Scala?
编辑/实用链接:
- https://github.com/rbricks/itemized
- http://pedrorijo.com/blog/scala-enums/
推荐答案
object Unit extends Enumeration {
abstract class UnitValue(var name: String) extends Val(name) {
def m: Unit
}
val G = new UnitValue("g") {
def m {
println("M from G")
}
}
val KG = new UnitValue("kg") {
def m {
println("M from KG")
}
}
}
这篇关于如何在Scala中添加枚举方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文