Selasa, 11 Januari 2011

Program konsole sederhana Scala

$ cat main.sh
#!/bin/sh

scala -classpath TraitTest.jar com.ykishi.scalatest.Main

$ ./main.sh
Hello!
doing action!
---------------------------------------------
BEFORE doAction()
doing action!
AFTER doAction()

$ more *.scala
::::::::::::::
Main.scala
::::::::::::::
package com.ykishi.scalatest

object Main {
def main(args :Array[String]) {

println( "Hello!" );

val action1 = new MyAction
action1.doAction

println("---------------------------------------------")
//
// In case when weaving is required, you can mix-in a specific trait as you prefer
//
val action2 = new MyAction with TWeaver
action2.doAction

}
}
::::::::::::::
MyAction.scala
::::::::::::::
package com.ykishi.scalatest

class MyAction extends TAction {
def doAction = { println("doing action!" ) }
}
::::::::::::::
TAction.scala
::::::::::::::
package com.ykishi.scalatest

trait TAction {
def doAction
}
::::::::::::::
TWeaver.scala
::::::::::::::
package com.ykishi.scalatest

trait TWeaver extends TAction {
abstract override def doAction {
println("BEFORE doAction()")

super.doAction

println("AFTER doAction()")
}
}

Tidak ada komentar:

Posting Komentar