first basic grammar

scala is a statically typed language. 
scalac aaa.scala compile scala
scala aaa.scala run scala

values ​​and variables:
val x:Int = 1 val x = 1 (type inferred)
var x:Int = 2 var x = 2
lazy value
a:Int = <lazy> //Defined now, load the
method definition when using:
def method name (parameter: parameter type): return type = {
//The last line in the block returns the value
}
When the return value is Unit, it can be defined as:
def method name (parameter name: parameter type) {}
There is no static method in scala, and similar things are implemented by object.
Conditional expression:
if(x > 0) 1 else -1
val a = if(x > 0) 1 else -1
For loop:
to: 1 to 5 Generates: Range(1,2,3,4,5)
until : 1 until 10 Generate: Range(1,2,3,4)
Range: Range(1,5) Generate: Range(1,2,3,4)
Range(1,5,2) Specify the step size
for(i <- 1 to 10){}
for(i <-1 until 10){}
for(i <- 1 to 10 if i %2 ==0){} //Add condition to i
for without continue and break
parameter style:
1: default parameter
def sayName( name : String = "lxm"){}
sayName("xj") sayName()
2: parameter with name
def add(x : Int , y : Int) : Int = {
x+y
}
add(y = 2 , x = 5)
3: Variable length parameters:
def sum(slems : Int*){}
sum(1,2,3,4,5)
Exception handling:
try{
brock(redis)
}catch{
case e : Exception =>System .err.println(e) //Pattern matching
case _ => //None of the above
}finally{
this.close(pool,redis)
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325168097&siteId=291194637