Ger*_*mán 7 scala equality case-class
我必须在这里遗漏一些愚蠢的东西.我有这个:
case class Color(val rgb:Int) {
private val c = rgb - 0xff000000
val r = (c & 0xff0000) >> 16
val g = (c & 0x00ff00) >> 8
val b = (c & 0x0000ff)
}
case object Red extends Color(0xffff0000)
case object Green extends Color(0xff00ff00)
case object Blue extends Color(0xff0000ff)
Run Code Online (Sandbox Code Playgroud)
然后我希望这打印真实:
val c = Color(0xff00ff00)
println(c == Green)
Run Code Online (Sandbox Code Playgroud)
为什么不呢?
4e6*_*4e6 13
从case类继承的Case类(或对象)是一种不好的做法,从Scala 2.9.1开始是非法的.使用object而不是case object定义Red,Green和Blue.