Skip to content

Instantly share code, notes, and snippets.

@colinpollock
Created April 28, 2012 01:27
Show Gist options
  • Save colinpollock/2514911 to your computer and use it in GitHub Desktop.
Save colinpollock/2514911 to your computer and use it in GitHub Desktop.
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