Reputation: 13101
I have a trait and trying to implement it in Main
object. Is this possible and what is the best practice?
trait PrintThis {
def nowPrint (thing:String)
}
object Main extends PrintThis {
def main(args: Array[String]): Unit = {
def nowPrint(thing:String): Unit = {
println("subject" )
}
nowPrint("test")
}
}
If that is not possible - is this a better approach (extend
trait in another class and implement trait method inside another class and then extend
that class in Main class and call the method)?
trait One {
def show()
}
class Two extends One {
def show() {println ("This is a show!") }
}
object Main extends Two {
def main(args: Array[String]): Unit = {
show()
}
}
Or maybe is better instantiating that new class inside Main and not extending
it?
object Main {
def main(args: Array[String]): Unit = {
var pointer:Two = new Two
pointer.show()
}
}
Upvotes: 0
Views: 302
Reputation: 1954
No this is not a good approach. Main function is meant to start running your application. Why would you need to extend any class from Main? If you want to use other classes, simply call their objects.
Upvotes: 1