1.简化代码

package com.scala.first

import java.io.File
import javax.management.Query /**
* Created by common on 17-4-5.
*/
object FileMatcher { def main(args: Array[String]) { for (file <- filesHere)
println(file) println() for (file <- filesMatching("src", _.endsWith(_)))
println(file) for (file <- filesEnding("src"))
println(file) } private def filesHere = (new File(".")).listFiles //matcher是传入一个函数,返回boolean值,比如_.endsWith(_)
private def filesMatching(query: String, matcher: (String, String) => Boolean) = {
for (file <- filesHere; if matcher(file.getName, query)) yield file
} //上面的函数不够简洁,下面是更加简洁的定义
private def filesMatch(matcher: String => Boolean) = {
for (file <- filesHere; if matcher(file.getName)) yield file
} //然后可以定义使用不同matcher()的方法
def filesEnding(query: String) = {
filesMatch(_.endsWith(query))
} //使用exists来简化代码
def containsOdd(nums: List[Int]): Boolean = {
nums.exists(_ % 2 == 1)
} def containsNeg(nums: List[Int]): Boolean = {
nums.exists(_ < 0)
} }

输出是

./.idea
./build.sbt
./target
./input
./project
./src ./src
./src

2.柯里化

//柯里化,可以看成是两个函数,y是第一个函数sum(x: Int)的参数
def sum(x: Int)(y: Int) = {
x + y
} //println(sum2(2))的输出是4
def sum2 = sum(2)_

3.继承

package com.scala.first

/**
* Created by common on 17-4-16.
*/
object AbstractClass { def main(args: Array[String]): Unit = {
val ae = new ArrayElement(Array("hello", "world"))
println(ae.width) val list1 = List(1,2,3,4)
val list2 = List(1,2,3,4,5)
//Scala中同时遍历两个list,如果长度不一样会截去
for((line1,line2) <- list1 zip list2){
println(line1+line2)
}
} } //定义一个抽象类
abstract class Element { //定义无参方法,抽象成员
def contents: Array[String] def height: Int = contents.length def width: Int = if (height == 0) 0 else contents(0).length } //扩展类,继承了上面的抽象类,需要实现抽象类中的方法
class ArrayElement(content: Array[String]) extends Element {
def contents: Array[String] = content
} //更加简洁的写法,contents和Element中的contents保持一致
class ArrayElement2(val contents: Array[String]) extends Element //另一个例子
class cat {
//确保一个成员不被子类重写,需要把其定义成final
// final val dangerous = false
val dangerous = false
} class tiger(override val dangerous: Boolean, private var age: Int) extends cat

4.特质

package com.scala.first

/**
* Created by common on 17-4-17.
*/
object Trait { def main(args: Array[String]): Unit = {
val person = new Person //变量an可以初始化为任何混入了特质的类的对象,person对象包含了Animal特质
val an: Animal = person
println(an.toString) val p = new PP()
//输出People特质中的内容
p.say() } } trait Animal { def run(): Unit = {
println("Animal can run")
}
} //具有特质Animal
class Person extends Animal {
override def toString = "can say"
} //具有特质Animal
class Tiger extends Animal {
override def toString = "can run fast"
} //一个类只能继承一个父类。但是能混入多个特质
//特征的作用:
//1.把胖接口转换成瘦接口
//2.为类提供可堆叠的改变
class Live trait HasLeg //注意不能给特质中传递任何的参数
class PersonTiger extends Live with Animal with HasLeg {
println("混入了多个特质")
} class P {
def say(): Unit = {
println("I am a people")
}
} //特质在抽象方法中的动态绑定
trait People extends P {
abstract override def say(): Unit = {
println("I am a trait people")
}
} class PP extends People{
//输出People特质中的内容
}

Scala学习笔记——简化代码、柯里化、继承、特质的更多相关文章

  1. 从0开始的Python学习007函数&函数柯里化

    简介 函数是可以重用的程序段.首先这段代码有一个名字,然后你可以在你的程序的任何地方使用这个名称来调用这个程序段.这个就是函数调用,在之前的学习中我们已经使用了很多的内置函数像type().range ...

  2. Scala基础:闭包、柯里化、隐式转换和隐式参数

    闭包,和js中的闭包一样,返回值依赖于声明在函数外部的一个或多个变量,那么这个函数就是闭包函数. val i: Int = 20 //函数func的方法体中使用了在func外部定义的变量 那func就 ...

  3. scala面向对象.高阶函数,柯里化,Actor编程简介

    1.定义一个类 class Person{ //用val修饰的变量是只读属性,有getter但是没有setter val id ="111" //用var修饰的变量既有getter ...

  4. Scala学习笔记(三)类层级和特质

    无参方法 功能:将方法的定义转换为属性字段的定义: 作用范围:方法中没有参数,并且方法仅能通过读取所包含的对象属性去访问可变状态,而不改变可变状态,就可使用无参方法: 例子: abstract cla ...

  5. (三十八)js之柯里化

    先给大家介绍什么是柯里化与反柯里化 百度翻译: 在计算机科学中,柯里化(Currying)是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数且返回结果的 ...

  6. Scala学习笔记(七):Rational、隐式转换、偏函数、闭包、重复参数及柯里化

    class Rational(n: Int, d: Int) { require(d != 0) private val g: Int = gcd(n, d) val number: Int = n ...

  7. 【React】学习笔记(一)——React入门、面向组件编程、函数柯里化

    课程原视频:https://www.bilibili.com/video/BV1wy4y1D7JT?p=2&spm_id_from=pageDriver 目录 一.React 概述 1.1.R ...

  8. Scala学习之路 (七)Scala的柯里化及其应用

    一.概念 柯里化(currying, 以逻辑学家Haskell Brooks Curry的名字命名)指的是将原来接受两个参数的函数变成新的接受一个参数的函数的过程.新的函数返回一个以原有第二个参数作为 ...

  9. 大数据学习day17------第三阶段-----scala05------1.Akka RPC通信案例改造和部署在多台机器上 2. 柯里化方法 3. 隐式转换 4 scala的泛型

    1.Akka RPC通信案例改造和部署在多台机器上  1.1 Akka RPC通信案例的改造(主要是把一些参数不写是) Master package com._51doit.akka.rpc impo ...

随机推荐

  1. LED类代码

      /* led.c文件 标题: 点亮一个了LED灯 电路:开发板中P2口已接到LED灯的阴极 */ #include <reg52.h> #include "led1.h&qu ...

  2. flask之wtform与flask-session组件

    1.wtform from flask import Flask, render_template, request, redirect from wtforms import Form from w ...

  3. If you sleep now,you will have a dream. If you study now,you will achieve your dream.

    If you sleep now,you will have a dream. If you study now,you will achieve your dream. 我开始思考,What's m ...

  4. rabbitmq使用方法(一)

    Introduction RabbitMQ is a message broker. The principal idea is pretty simple: it accepts and forwa ...

  5. unity3d脚本语言中的引用类型

    在之前的文文里有说到,值类型和引用类型,那么这会就单独说下引用类型: Unity3D中的C#语言提供了专门的类型来为开发者提供使用C#开发游戏的便利条件: 在该引擎中,使用UnityEngine命名空 ...

  6. Delphi的接口委托示例

    {  说明:该事例实现的效果,在单个应用或代码量小的项目中,可以完全不用接口委托来完成.  之所以采用委托接口,主要是应用到:已经实现的接口模块中,在不改变原有代码的情况下,  需要对其进行扩展:原始 ...

  7. VS2013中Python学习笔记[环境搭建]

    前言 Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有比其他语言更有特色 ...

  8. 关于VIM自动缩进失效(filetype indent on无效)的详细分析

    关于VIM自动缩进失效(filetype indent on无效)的详细分析 set filetype=xml filetype indent on 执行对齐命令:ggvG

  9. 解决Nginx的13: Permission denied) while connecting to upstream

    一.问题 做Nginx负载的时候,经常遇到这样的情况: // :: [crit] #: * connect() to failed (: Permission denied) while connec ...

  10. JAVA和C#检测IP地址段是否交叉和获取地址段IP列表的方法

    一.说明 我们经常编程时,需要对一个DIDR地段计算其可用IP地址,或者验证某个IP是否被包含在一个地址段中. 二.工具 1.Java 可以使用 cidr-ip-trie库解决. https://gi ...