@@ -18,22 +18,22 @@ package s4j.scala.chapter18
1818
1919object HeroConstructorPatternExample extends App {
2020
21- val bruce = new SuperHero (" Batman" , " Bruce Wayne" , List (" Intellect " , " Speed" , " Agility" , " Strength " ))
22- val youngBruce = new Person ( " Bruce Wayne " )
21+ val bruce = new SuperHero (" Batman" , " Bruce Wayne" , List (" Speed" , " Agility" ))
22+ val steve = new SuperHero ( " Captain America " , " Steve Rogers " , List ( " Tactics " , " Speed " ) )
2323 val jane = new Person (" Jane Doe" )
2424
25+
2526 def superPowersFor (person : Person ) = {
2627 person match {
27- case SuperHero (_, name , powers) => powers
28- case _ => List ()
28+ case SuperHero (_, _ , powers) => powers
29+ case _ => List ()
2930 }
3031 }
3132
32- // Example of a constructor pattern
33+ // What super-powers does an otherwise unknown person have, if they are a
34+ // superhero with the alter-ego Bruce Wayne?
3335
34- // What super powers does a person have, if they are in fact a super hero who's alter ego is Bruce Wayne?
35- val person = bruce
36- println(" Bruce has the following powers " + superPowersFor(person))
37- println(" Young Bruce has the following powers " + superPowersFor(youngBruce))
38- println(" Jane has the following powers " + superPowersFor(jane))
36+ println(" Bruce has the following powers: " + superPowersFor(bruce).mkString(" , " ))
37+ println(" Steve has the following powers: " + superPowersFor(steve).mkString(" , " ))
38+ println(" Jane has the following powers: " + superPowersFor(jane).mkString(" , " ))
3939}
0 commit comments