The Apply method is a very common approach in Scala, and you can use this as an overloaded form of the () operator.
The Apply method for the associated object, like the above, is a common technique for building objects in Scala and no longer requires new.
The value of an if condition expression is the value of an expression that follows if or else, and if the two types are different, select the public superclass of each branch type as the return type.
if (x>1) 1 equals if (x>1) 1 else ()
You can think of () as a placeholder for "no useful value", and the unit as a void in Java
If you are writing a long statement that requires a branch to write, make sure that the first line ends with a symbol that cannot be used as the end of the statement. In general, a good choice is an operator:
"AA" + "BB"
+ "CC"// this parser will error
"AA" + "BB" +
"CC"//This is the right
The Scala expression uses {}, and the value of the last expression is the value of the block.
Scala loop while and for:
Val A = for (I <-1 to ten) {i}
Val B = for (i <-1 until) {i}
Scala Break:
Import Scala.util.control.breaks._object Breakdemo { def main (args:array[string]): Unit = { breakable { for (I <-1 to ten) {Break}}} }
Scala for derivation, returns a vector collection
Vector (1, 2, 3, 4, 5, 6, 7, 8, 9, ten) val a = for (I <-1 to ten) yield {i}
The named parameter of the Scala function
def decorate (atr:string, left:string = "[", right:string = "]") = {left + ATR + right }decorate ("Hello" , right = ">>>]")
Scala variable length parameters
def writename (name:string*) = {for (n <-name) { println (n) } } val se = Array ("AA", "BB", "CC") C6/>writename (SE: _*)
When Val is declared as lazy, initialization is deferred, knowing that the value was first taken.
Scala exception
Package Com.citi.Lscalaobject Fundemo { def main (args:array[string]): Unit = { try { // Vector (1, 2, 3, 4 , 5, 6, 7, 8, 9, ten) val a = for (I <-1 to ten) yield {i} println (a) println (Decorate ("Hello", ">>& gt;])) Val B = -10 println (CheckValue (b). GetClass ()) println (Decorate ("Hello", right = ">>>]")) println (1 to 5) val se = Array ("AA", "BB", "CC") writename (se: _*) } catch {case t:throwable = = . Printstacktrace () Case _: Exception = println ("Exception") } finally { println ("do finally") } } def checkvalue (x:int) = { if (x>1) 1 else () } def decorate (atr:string, left:string = "[", Right:stri ng = "]") = {left + ATR + right } def writename (name:string*) = {for (n <-name) { pri NTLN (n) }}}
Learn Scala lesson two (apply, if expression, loop, function's name parameter, variable length parameter, exception)