Objektorientierte Programmierung WS 2013/2014 - Datei first.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 AnyRef {
def lastName = ln
def firstName = fn
def spouse = s
def this(ln : String, fn : String) = { this(ln, fn, null) }
private def isMarried() = {spouse != null}
def computeTax(income: Double) : Double = {
if (isMarried()) income * 0.20 else income * 0.30
}
override def 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))
}
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 20.01.2014


