zl程序教程

您现在的位置是:首页 >  其它

当前栏目

插值,多行字符串,匿名函数

函数 字符串 匿名 多行 插值
2023-09-14 09:14:46 时间
object StringApp extends App{
  val s1="hello:"
  val name="张三"
  println(s1+name)
//插值
  println(s"hello:$name")//这里面的s是固定写法,s+$一起使用
  val team="AC Milan"
  println(s"hello:$name,welcome to $team")
//多行字符串
  val b=           //按住shift+双引号,按三次双引号
    """
      |这是一个多行字符串
      |hello
      |world
      |张三
      |""".stripMargin

  println(b)
}
/**
 * 匿名函数:函数可以命名也可以不命名
 * (参数名:参数类型...)=>函数体,括号必须有
 */
object FunctionApp extends App {
  (x:Int)=>x+1
  val m1=(x:Int)=>x+1//将匿名函数传给一个变量
  m1(10)//使用匿名函数
  
//普通函数来完成
  def add(x:Int,y:Int): Unit ={
    x+y
  }
//匿名函数来完成
  def add1=(x:Int,y:Int)=>x+y
  
//调用匿名函数add1  
  add1(2,6)
}