初识Scala
val 与 var
val
常量:相当于使用final
修饰的变量
var
用于声明变量
object 与 class
object
-
Scala的object就相当于一个单例的对象,即
static class
修饰 -
Object内的方法是直接可以运行的(不需要new一个对象),所以main方法必须在object
-
object允许裸露代码,相当于object将代码放到了static静态代码块中
class
- class内的方法是必须要创建对象后才能使用的
- 类里允许裸露代码,其字节码实现是将其放在构造函数中的
- 类名构造器(写在类名旁边的属性)就是默认构造器,没有类名构造器即为默认无参构造器,其他重载构造器必须调用类名构造器(默认构造器)
- 类名构造器中的参数就是类的成员属性,且默认是
private val
修饰 - 方法函数参数都是默认val修饰,且不允许修改成var,只有在类名构造器中的参数可以设置成var
伴生
如果object与class的名称相同,则二者为伴生关系,即class所new出来的对象即为伴生对象,可以访问同名object的属性和方法
万物皆对象
- Any为所有对象的超类
- AnyVal为原本Java基本数据类型 + Unit(null)的父类
- AnyRef为原本Java引用类型的超类,即
java.lang.Object
循环
while循环
while(condition) {
statement(s);
}
for循环
for( var x <- Range ){
statement(s);
}
Range:以上语法中,Range 可以是一个数字区间表示 i to j ,或者 i until j。左箭头 <- 用于为变量 x 赋值。
i to j:为
[i, j]
的集合i until j:为
[i, j)
的集合
在 for 循环 中你可以使用分号 (😉 来设置多个区间,它将迭代给定区间所有的可能值。以下实例演示了两个区间的循环实例:
object Test {
def main(args: Array[String]) {
var a = 0;
var b = 0;
// for 循环
for( a <- 1 to 3; b <- 1 to 3){
println( "Value of a: " + a );
println( "Value of b: " + b );
}
}
}
迭代集合:
for( x <- List ){
statement(s);
}
for循环过滤,Scala 可以使用一个或多个 if 语句来过滤一些元素。以下是在 for 循环中使用过滤器的语法。
for( var x <- List
if condition1; if condition2...
){
statement(s);
}
yield
你可以将 for 循环的返回值作为一个变量存储。语法格式如下:
var retVal = for{ var x <- List
if condition1; if condition2...
}yield x
object Test {
def main(args: Array[String]) {
var a = 0;
val numList = List(1,2,3,4,5,6,7,8,9,10);
// for 循环
var retVal = for{ a <- numList
if a != 3; if a < 8
}yield a
// 输出返回值
for( a <- retVal){
println( "Value of a: " + a );
}
}
}
函数
函数的定义:
def 函数名(参数名:变量类型……): 返回值类型 = {函数体}
在Scala语言中可以省略return语句,默认在函数执行的最后一行,即为本函数的返回值。
递归函数
package pro.eddievim.func
/**
* @Author eddieVim
* @微信公众号 艾迪威姆 / PositiveEddie
* @Blog https://blog.csdn.net/weixin_44129784
* @Create 2020/12/3 12:34
* @Discription
*/
object recursive {
def main(args: Array[String]): Unit = {
println(recursive(100))
}
/**
* 使用递归解法,计算 1 ~ num 的和
* @param num
*/
def recursive(num:Int): Int = {
if (num == 1) {
num
} else {
num + recursive(num - 1)
}
}
}
默认值函数
package pro.eddievim.func
/**
* @Author eddieVim
* @微信公众号 艾迪威姆 / PositiveEddie
* @Blog https://blog.csdn.net/weixin_44129784
* @Create 2020/12/3 12:53
* @Discription
*/
object defaultVal {
def main(args: Array[String]): Unit = {
// No:1 str:OK
defaultVal(1, "OK")
// No:666 str:default
defaultVal()
// No:999 str:default
defaultVal(999)
// No:666 str:YES
defaultVal(str = "YES")
}
def defaultVal(i:Int = 666, str:String = "default"): Unit = {
println(s"No:$i str:$str")
}
}
匿名函数
val fun: (Int, Int) => Int = (a Int, b Int) => {a + b}
-
函数是第一类值(即:函数也可以复制给变量进行传递)
-
函数签名:
(Int, Int) => Int
-
匿名函数体:
(a Int, b Int) => {a + b}
嵌套函数
package pro.eddievim.func
/**
* @Author eddieVim
* @微信公众号 艾迪威姆 / PositiveEddie
* @Blog https://blog.csdn.net/weixin_44129784
* @Create 2020/12/3 13:07
* @Discription
*/
object nest {
def main(args: Array[String]): Unit = {
// func1:hello, world!
print("func1:")
function1("hello, world!")
// func2:
print("func2:")
function2("hello, world!")
}
def function1(str:String): Unit = {
def nest(): Unit = {
// 可以访问外部函数的参数
println(str)
}
// 执行函数
nest()
}
def function2(str:String): Unit = {
def nest(): Unit = {
println(str)
}
}
}
偏应用函数
给多参的函数加上默认的参数,使其变成一个“新的函数”
package pro.eddievim.func
import java.util.Date
/**
* @Author eddieVim
* @微信公众号 艾迪威姆 / PositiveEddie
* @Blog https://blog.csdn.net/weixin_44129784
* @Create 2020/12/3 13:23
* @Discription
*/
object PartialAppliedFunction {
def main(args: Array[String]): Unit = {
val date = new Date()
// Thu Dec 03 13:27:17 CST 2020 0 OK
partialAppliedFunction(date, 0, "OK")
// 偏应用函数
// Thu Dec 03 13:27:17 CST 2020 -1 worried
val f1 = partialAppliedFunction(_:Date, -1, _:String)
f1(date, "worried")
// Thu Dec 03 13:27:17 CST 2020 -1 worried
val f2 = partialAppliedFunction(_:Date, -1, "worried")
f2(new Date())
}
def partialAppliedFunction(date:Date, code:Int, msg:String): Unit = {
println(s"$date\t$code\t$msg")
}
}
可变长参数函数
package pro.eddievim.func
/**
* @Author eddieVim
* @微信公众号 艾迪威姆 / PositiveEddie
* @Blog https://blog.csdn.net/weixin_44129784
* @Create 2020/12/3 13:34
* @Discription
*/
object VariableLength {
def main(args: Array[String]): Unit = {
// 1
variableLengthFunction(1)
// 1 2 3
variableLengthFunction(1, 2, 3)
}
def variableLengthFunction(list:Int*): Unit = {
list.foreach((x) => {print(x + "\t")})
}
}
高阶函数
函数作为参数,函数作为返回值
package pro.eddievim.func
/**
* @Author eddieVim
* @微信公众号 艾迪威姆 / PositiveEddie
* @Blog https://blog.csdn.net/weixin_44129784
* @Create 2020/12/3 13:45
* @Discription
*/
object HigherOrderFunction {
def main(args: Array[String]): Unit = {
// 3
computer(1, 2, add)
// 2
computer(1, 2, multiply)
}
/**
* 用于对a变量与b变量的计算
*
* @param a
* @param b
* @param fun
*/
def computer(a:Int, b:Int, fun:(Int, Int) => Int): Unit = {
println(fun(a, b))
}
// 两数之和
def add(a:Int, b:Int): Int = {
a + b
}
def multiply(a:Int, b:Int): Int = {
a * b
}
}
柯里化
柯里化(Currying)指的是将原来接受多个个参数的函数变成新的接受一个一个参数的函数的过程。新的函数返回一个以原有第二个参数为参数的函数。
package pro.eddievim.func
/**
* @Author eddieVim
* @微信公众号 艾迪威姆 / PositiveEddie
* @Blog https://blog.csdn.net/weixin_44129784
* @Create 2020/12/3 14:14
* @Discription
*/
object Currying {
def main(args: Array[String]): Unit = {
currying(1,2,3)("666", "OK", "perfect")
}
def currying(ints:Int*)(strs:String*): Unit = {
ints.foreach(x => print(s"$x\t"))
strs.foreach(x => print(s"$x\t"))
}
}