I have started playing with Scala so my code is a bit Java-ish. Below is my code :
object TypeParam extends App{
class Param1[T](val elem: T*){
val elems : List[T] = elem.toList
def toPrint(): Unit ={
elems.foreach(print)
}
}
@Override
def main(args : List[String]): Unit ={
val x= new Param1[Int](1,2,3,4,6,7)
x.toPrint()
}
}
what I am expecting is to get the elements printed but in realty I am not getting any output.
I know that I can print a list easily through forEach
but for learning I am trying to blend things up.
Could anyone explain why I am not getting any output,so in scala do we not have class level variable which can be set ?
By using below syntax you are not overriding main
method from App
trait, hence you do not see any output.
@Override
def main(args : List[String]): Unit ={
Use below syntax to override main
method:
override def main(args: Array[String]) {
Note use of override
keyword. In Scala, it's a mandatory to use override
when overriding a concrete method.
I think the problem with main
method. As you're overriding App
trait
you can put code, you want to execute, in the body of class itself, instead of creating specific method.
Original main
in App looks slightly different:
def main(args : scala.Array[scala.Predef.String]) : scala.Unit
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.