美文网首页
_下划线的使用

_下划线的使用

作者: 冰冰大象 | 来源:发表于2018-03-06 11:22 被阅读31次
  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: _*)

相关文章

  • pythonic

    1.下划线的用法 只在类的内部使用双下划线image.png 只在模块内使用单下划线(外部import不了)ima...

  • Word下划线怎么打?这三种方法你都知道吗?

    下划线的使用在Word中比较常见的,但对于下划线的使用你真的会吗?所以今天就与大家分享一下Word下划线的相关技巧...

  • tableView取消cell默认下划线

    一般使用 隐藏表格所有的下划线,在重写UITableViewCell底部的下划线

  • Github中写文档时的技巧

    markdown引用块内换行:用html:在需要换行的段落前使用 类似的下划线也是: 下划线 产生 下划线 mar...

  • _下划线的使用

    存在性类型:Existential typesval site = List(Option("Runoob"),O...

  • JavaScript入门基础

    定义变量的关键词是var 1. 必须使用字母,下划线,或者$开始 2. 使用多个字母,下划线,数字,$组合 3. ...

  • python中下划线命名的含义

    python中有五种下划线命名;(1)单前导下划线:var以单个下划线开头的变量或方法仅供内部使用,但是不强制执行...

  • EditText点击隐藏hint

    EditText使用很广泛,今天使用中需要去掉光标以及下划线,还需要点击后隐藏hint 去掉下划线 android...

  • swift4-标识符和关键字

    标识符可以是下划线、字母或数字,并且可以使用下划线作为标识符的开始字符 //以下划线开头的标识符let _numb...

  • bug合集

    记一个bug :2020-07-01 在使用下划线时遇到换行后下划线不显示的问题,刚开始以为是自己的下划线没有设置...

网友评论

      本文标题:_下划线的使用

      本文链接:https://www.haomeiwen.com/subject/uwtooxtx.html