Created
April 28, 2012 01:27
-
-
Save colinpollock/2514911 to your computer and use it in GitHub Desktop.
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
class Person(val name: String) { | |
def greeting = "I am Person %s".format(name) | |
override def equals(that: Any): Boolean = that match { | |
case p: Person if p.name == this.name => true | |
case _ => false | |
} | |
override def hashCode = 11 | |
} | |
case class Worker ( | |
override val name: String, | |
job: String | |
) extends Person(name) { | |
override def greeting = "I am Worker %s".format(name) | |
override def equals(that: Any): Boolean = that match { | |
case w: Worker if w.name == this.name && w.job == this.job => true | |
case _ => false | |
} | |
override def hashCode = 22 | |
} | |
object Instance { | |
def process(p: Person) = println(p.greeting) | |
def personHash(p: Person) = p.hashCode | |
def equalsColin(p: Person) = { | |
val colin = new Person("colin") | |
println("Equals Colin? %s".format(colin == p)) | |
println("ColinEquals? %s".format(p == colin)) | |
} | |
def main(args: Array[String]){ | |
val colinWorker = Worker("colin", "coder") | |
process(colinWorker) | |
println("colinworker hash code: %d".format(colinWorker.hashCode)) | |
println("personHash for colin worker: %d".format(personHash(colinWorker))) | |
equalsColin(colinWorker) | |
} | |
} | |
Instance.main(Array.empty[String]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment