Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
21
Task/Polymorphic-copy/Scala/polymorphic-copy.scala
Normal file
21
Task/Polymorphic-copy/Scala/polymorphic-copy.scala
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
object PolymorphicCopy {
|
||||
|
||||
def main(args: Array[String]) {
|
||||
val a: Animal = Dog("Rover", 3, "Terrier")
|
||||
val b: Animal = a.copy() // calls Dog.copy() because runtime type of 'a' is Dog
|
||||
println(s"Dog 'a' = $a") // implicitly calls Dog.toString()
|
||||
println(s"Dog 'b' = $b") // ditto
|
||||
println(s"Dog 'a' is ${if (a == b) "" else "not"} the same object as Dog 'b'")
|
||||
}
|
||||
|
||||
case class Animal(name: String, age: Int) {
|
||||
|
||||
override def toString = s"Name: $name, Age: $age"
|
||||
}
|
||||
|
||||
case class Dog(override val name: String, override val age: Int, breed: String) extends Animal(name, age) {
|
||||
|
||||
override def toString = super.toString() + s", Breed: $breed"
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue