Advertisement
Guest User

Untitled

a guest
Oct 16th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.82 KB | None | 0 0
  1. //Document.scala
  2. class Document {
  3.   private var title = ""
  4.   private var author = ""
  5.   // These methods return this.type so they can be used
  6.   // for method chaining, even with subclasses
  7.   def setTitle(title: String): this.type = { this.title = title; this }
  8.   def setAuthor(author: String): this.type = { this.author = author; this }
  9.   override def toString = getClass.getName + "[title=" + title + ",author=" + author + "]"
  10. }
  11.  
  12. class Book extends Document {
  13.   private var chapters = new scala.collection.mutable.ArrayBuffer[String]
  14.   def addChapter(chapter: String) = { chapters += chapter; this }
  15.   override def toString = super.toString + "[chapters=" + chapters + "]"
  16. }
  17.  
  18. object Main extends App {
  19.   val book = new Book
  20.   book.setTitle("Scala for the Impatient").addChapter("Chapter 1 ...")
  21.   println(book)
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement