函数语法

This commit is contained in:
2018-11-25 20:27:31 +08:00
commit e9f97b64fa
5 changed files with 168 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
package top.fjy8018
/**
* @author ${user.name}
*/
object App {
def foo(x : Array[String]) = x.foldLeft("")((a,b) => a + b)
def main(args : Array[String]) {
println( "Hello World!" )
println("concat arguments = " + foo(args))
}
}

View File

@@ -0,0 +1,32 @@
package top.fjy8018.scala
/**
* F嘉阳
* 2018-11-25 20:21
*/
object FunctionApp {
def main(args: Array[String]): Unit = {
println(add(1,2))
println(sum(3,4))
// 调用无参方法可以不加括号
printHello
}
def add(x:Int,y:Int):Int={
// 函数最后一行为返回值
x+y
}
/**
* 函数体只有一行可以省略括号返回值scala会自动进行类型推断
* @param x
* @param y
* @return
*/
def sum(x:Int,y:Int)= x + y
def printHello()= println("Hello world!")
}

View File

@@ -0,0 +1,7 @@
package top.fjy8018.scala
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello world")
}
}