函数语法
This commit is contained in:
15
src/main/scala/top/fjy8018/App.scala
Normal file
15
src/main/scala/top/fjy8018/App.scala
Normal 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))
|
||||
}
|
||||
|
||||
}
|
||||
32
src/main/scala/top/fjy8018/scala/FunctionApp.scala
Normal file
32
src/main/scala/top/fjy8018/scala/FunctionApp.scala
Normal 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!")
|
||||
|
||||
}
|
||||
7
src/main/scala/top/fjy8018/scala/HelloWorld.scala
Normal file
7
src/main/scala/top/fjy8018/scala/HelloWorld.scala
Normal file
@@ -0,0 +1,7 @@
|
||||
package top.fjy8018.scala
|
||||
|
||||
object HelloWorld {
|
||||
def main(args: Array[String]): Unit = {
|
||||
println("Hello world")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user