Universität Paderborn - Home Universität Paderborn
Die Universität der Informationsgesellschaft

Objektorientierte Programmierung WS 2013/2014 - Datei first-sol.scala

trait Log {
  def logMethod(s: String) {
     println("entering method " + s)
  }
  def logConstructor(s: String) {
     println("entering constructor " + s)
  }
}

class Person(ln : String, fn : String, s : Person) extends Log {
  def lastName = ln
  def firstName = fn
  def spouse = s
  logConstructor("(string X string X Person)")
  
  def this(ln : String, fn : String) = { 
                                         this(ln, fn, null) 
					 logConstructor("(string X string)")
				       }
  def this() = { 
                 this("Mustermann", "Max", null)
		 logConstructor("()")
	       }

  private def isMarried() = { logMethod("isMarried")
                              spouse != null
			    }

  def computeTax(income: Double) : Double = {
      logMethod("computeTax")
      if (isMarried()) income * 0.20 else income * 0.30
  }

  override def toString() = {
       logMethod("toString")
      "Hi, my name is " + firstName + " " + lastName +
      (if (spouse != null) " and this is my spouse, " + spouse.firstName + " " + spouse.lastName + "." else ".");
  }
}

object Willy {
   def main( a : Array[String]) {
      val p1 = new Person("Mann", "Willy", new Person("Mann", "Dora"))
      println(p1)
      println(p1.computeTax(42000))
      val p2 = new Person()
      println(p2)
   }
}

Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 21.01.2014