摘要: 能丰富现有类库功能,增强类的方法 隐式转换函数:以implicit关键字声明并带有单个参数的函数 其中用到了装饰模式,门面模式 阅读全文
posted @ 2019-08-16 17:04 刘宇石 阅读(185) 评论(0) 推荐(0) 编辑
摘要: 把接受多个参数的函数转变成接受单一参数的函数 阅读全文
posted @ 2019-08-16 15:28 刘宇石 阅读(121) 评论(0) 推荐(0) 编辑
摘要: // 声明高阶函数,又叫算子, 包含多余一个箭头的函数 val func: Int => Int = x => x * x val arr = Array(1, 2, 3, 4, 5, 6) // 函数可做为函数传入 arr.map(x => func(x)) arr.map(func) // 将方法转换成函数,作为传入参数,这里做了隐式转换 def m1(x: Int) = x * x arr. 阅读全文
posted @ 2019-08-16 14:37 刘宇石 阅读(149) 评论(0) 推荐(0) 编辑
摘要: // 偏函数 /** * PartialFunction[A, B], A 是参数类型, B 是返回值类型,PartialFunction 常用于模式匹配 */ object PartialFunctionDemo { // String 参数类型,Int返回类型 def m1: PartialFunction[String, Int] = { case "one" => { println(" 阅读全文
posted @ 2019-08-16 14:27 刘宇石 阅读(155) 评论(0) 推荐(0) 编辑
摘要: 匹配数组,元组,集合 模式守卫 为了让匹配更加具体,可以使用模式守卫,也就是在模式后面加上if <boolean expression>。 def showImportantNotification(notification: Notification, importantPeopleInfo: S 阅读全文
posted @ 2019-08-16 14:24 刘宇石 阅读(197) 评论(0) 推荐(0) 编辑
摘要: package day03 object TraitDemo { def main(args: Array[String]): Unit = { val human = new Human println(human.name) println(human.distance) println(human.fight) println(human.run()) } } /** * 特质 */ tra 阅读全文
posted @ 2019-08-16 11:13 刘宇石 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 伴生对象:与类名相同并且用Object修饰的对象, 类和伴生对象可以互相访问私有方法和属性 阅读全文
posted @ 2019-08-16 08:20 刘宇石 阅读(145) 评论(0) 推荐(0) 编辑