_下划线的使用

  1. 存在性类型:Existential types

    val site = List(Option("Runoob"),Option(12),None)
    //Option[_]表示Option集合里可以是各种类型,应为上面是字符串,数字类型,也可以用Option[Any]来替换
    def test(l:List[Option[_]]):Unit={
    for(v<-l)
        {
            print(v.getOrElse("默认值"))
        }
     }
    test(site)
    
  2. 临时变量:Ignored variables

     val _ = 5
    
  3. 临时参数:Ignored parameters

     List(1, 2, 3). foreach { _ => println("Hi") }
    
  4. 通配模式:Wildcard patterns

     Some(5) match { case Some(_) => println("Yes") }
     match {
      case List(1,_,_) => " a list with three element and the       first element is 1"
      case List(_*)  => " a list with zero or more elements "
      case Map[_,_] => " matches a map with any key type and any value type "
      case _ =>
    }
    
    //模式匹配       
     abstract class Item
     case class Product(description: String, price: Double)  extends Item
     case class Bundle(description: String, discount: Double, items: Item*) extends Item
    
     def price(it: Item): Double = it match {
     case Product(_, p) => p
     //这里注释下map(price _) 注意什么都没有,_表示的是Product对象,取他的属性price,不知道为什么要这么写~~
     case Bundle(_, disc, its @ _*) => its.map(price _).sum * (100-disc) /100
     //这里@表示将嵌套的值绑定到变量its
     }
     //测试
     val bun2 = Bundle("Appliance sale",10.0,Product("Haier Refrigerato", 3000.0),Product("Geli air conditionor",2000.0))
      println(price(bun2))
    
  5. 通配导入:Wildcard imports

         import java.util._
    
  6. 隐藏导入:Hiding imports

       // Imports all the members of the object Fun but renames Foo to Bar
     import com.test.Fun.{ Foo => Bar , _ }
    
       // Imports all the members except Foo. To exclude a member rename it to _
     import com.test.Fun.{ Foo => _ , _ }
    
  7. 连接字母和标点符号:Joining letters to punctuation

       def bang_!(x: Int) = 5
    
  8. 偏应用函数:Partially applied functions

    def fun = {
    // Some code
    }
    val funLike = fun _
    
    List(1, 2, 3) foreach println _
    
    1 to 5 map (10 * _)
    
    foo _               // Eta expansion of method into method value
    
    foo(_)              // Partial function application
    Example showing why foo(_) and foo _ are different:
    
     trait PlaceholderExample {
    def process[A](f: A => Unit)
    val set: Set[_ => Unit]
        set.foreach(process _) // Error 
        set.foreach(process(_)) // No Error
    }
    
  9. 初始化默认值:default value

    var i: Int = _
    
  10. 作为参数名:
    //访问map
    var m3 = Map((1,100), (2,200))
    for(e<-m3) println(e._1 + ": " + e._2)
    m3 filter (e=>e.1>1)
    m3 filterKeys (
    >1)
    m3.map(e=>(e._1*10, e._2))
    m3 map (e=>e._2)

//访问元组:tuple getters
(1,2)._2

  1. 参数序列:parameters Sequence
    *作为一个整体,告诉编译器你希望将某个参数当作参数序列处理。例如val s = sum(1 to 5:)就是将1 to 5当作参数序列处理。
    //Range转换为List
    List(1 to 5:_
    )

//Range转换为Vector
Vector(1 to 5: _*)

//可变参数中
def capitalizeAll(args: String*) = {
args.map { arg =>
arg.capitalize
}
}

val arr = Array("what's", "up", "doc?")
capitalizeAll(arr: _*)

你可能感兴趣的:(_下划线的使用)