Skip to content

Instantly share code, notes, and snippets.

@ot32em
Created May 19, 2015 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ot32em/685c7b546f5e74360580 to your computer and use it in GitHub Desktop.
Save ot32em/685c7b546f5e74360580 to your computer and use it in GitHub Desktop.
case class script
/**
* Created by OTChen on 2015/5/19.
*/
object Ch15_1 extends App {
println("parametric field access level")
//val red = new RedBull(100)
//red.energy
val blue = new BlueBull(90)
println("BlueBull energy: " + blue.energy)
println("\nequality")
val cats = (new Cat(20, true), new Cat(20, true))
println("cats._1 == cats._2: " + (cats._1 == cats._2))
val case_cats = (new CaseCat(20, true), new CaseCat(20, true))
println("case_cats._1 == case_cats._2: " + (case_cats._1 == case_cats._2))
println("\ntoString")
val (cat, _) = cats
val (case_cat, _) = case_cats
println("cat: " + cat)
println("case_cat: " + case_cat)
println("\nconstructor pattern")
// val getWeight = cat match {
// case Cat(w, b) => w
// }
// println("getWeight: " + getWeight)
val getCaseWeight = case_cat match {
case CaseCat(w, b) => w
}
println("getCaseWeight: " + getCaseWeight)
println("\nfactory method")
//val another_cat = Cat(30, true)
//println(another_cat)
val another_cat = new Cat(30, true)
val another_case_cat = CaseCat(30, true)
println("another_case_cat: " + another_case_cat)
val blackDog = BlackDog(100)
val whiteDog = WhiteDog(90)
def describeDog(dog: Dog): String = dog match {
case BlackDog(h) => h + " height dog"
}
println(describeDog(blackDog))
}
class RedBull(energy: Int)
case class BlueBull(energy: Int)
class Cat(val weight: Int, val isBlack: Boolean)
case class CaseCat(val weight: Int, val isBlack: Boolean)
abstract class Dog {
def color: String
}
case class BlackDog(height: Int) extends Dog { override def color = "black"}
case class WhiteDog(height: Int) extends Dog { override def color = "white"}
@ot32em
Copy link
Author

ot32em commented May 19, 2015

scalac Ch15_1.scala
scala Ch15_1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment