Created
February 13, 2012 18:19
-
-
Save alphaneet/1818798 to your computer and use it in GitHub Desktop.
classの引数でずらずらーと書きたくないでござる!つhttp://d.hatena.ne.jp/alpha_neet/20120213/1329157666
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Idenshi(id: Int) | |
class Animal(val i: Idenshi) | |
class Dog(i: Idenshi) extends Animal(i) | |
class Tiwawa(i: Idenshi) extends Dog(i) | |
val a = new Tiwawa(Idenshi(3)) | |
println(a.i) | |
/** | |
output: | |
Idenshi(3) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Idenshi(id: Int) | |
trait Animal { val i: Idenshi } | |
trait Dog extends Animal | |
trait Tiwawa extends Dog | |
val a = new Tiwawa { val i = Idenshi(3) } | |
println(a.i) | |
/** | |
output: | |
Idenshi(3) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Idenshi(id: Int) | |
@cloneable | |
trait Animal { | |
val i: Idenshi | |
override def hashCode = i.hashCode | |
override def equals(that: Any): Boolean = that match { | |
case x: Animal => i == x.i | |
case _ => false | |
} | |
override def clone: this.type = super.clone.asInstanceOf[this.type] | |
override def toString = i.toString | |
} | |
trait Dog extends Animal | |
trait Tiwawa extends Dog | |
def create(id: Int): Tiwawa = new Tiwawa { val i = Idenshi(id) } | |
val animals = scala.collection.mutable.Set[Animal]() | |
val a = create(3) | |
val a2: Tiwawa = a.clone | |
assert(a == a2) | |
assert(a == create(3)) | |
println(a.i) | |
// 同じと認識されるので何度 id=3 を足しても増えない | |
animals += create(3) | |
animals += create(3) | |
animals += create(4) | |
animals += create(3) | |
animals += create(3) | |
println(animals) | |
assert(animals.size == 2) | |
animals -= create(3) | |
println(animals) | |
assert(animals.size == 1) | |
/** | |
output: | |
Idenshi(3) | |
Set(Idenshi(4), Idenshi(3)) | |
Set(Idenshi(4)) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment