diff --git a/_includes/bullet-traits.html b/_includes/bullet-traits.html index 67f09673f..c372ca9bf 100644 --- a/_includes/bullet-traits.html +++ b/_includes/bullet-traits.html @@ -1,17 +1,39 @@ +{% comment %} +Borrowed from +http://gleichmann.wordpress.com/2009/10/21/scala-in-practice-composing-traits-lego-style/ +{% endcomment %} +
Traits
-
// A trait for printing an object to standard out.
-trait StdoutPrinter {
-  def print = println(this.toString)
+  
abstract class Spacecraft {
+  def engage(): Unit
 }
-
-// A common pattern:
-// Define a class with a primary constructor and assign its arguments to public members.
-case class Person(val name: String, val age: Int) {
-  override def toString = name + " is " + age + " years old"
+trait CommandoBridge extends Spacecraft {
+  def engage(): Unit = {
+    for (_ <- 1 to 3)
+      speedUp()
+  }
+  def speedUp(): Unit
 }
-
-val p = new Person("John Doe", 34) with StdoutPrinter
-p.print
+trait PulseEngine extends Spacecraft { + val maxPulse: Int + var currentPulse: Int = 0 + def speedUp(): Unit = { + if (currentPulse < maxPulse) + currentPulse += 1 + } +} +class StarCruiser extends Spacecraft + with CommandoBridge + with PulseEngine { + val maxPulse = 200 +}
+

+Multiple traits can be mixed in a class to combine their interface and their +behavior. +Here, a StarCruiser is a Spacecraft with a CommandoBridge that knows how to +engage the ship (provided a means to speed up) and a PulseEngine that +specifies how to speed up. +