如何在 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?
编辑/有用的链接:
推荐答案
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屋!
查看全文