列表
使用List(“”,“”,“”)去声明
sliding 和 groued表示迭代器
val iter = List("Hadoop", "Spark", "Scala") sliding 2// sliding 和 groued 是有区别的while (iter.hasNext){println(iter.next())}for (elem <- iter){println(elem)}
可变数组
def main(args: Array[String]): Unit = {import scala.collection.mutable.ArrayBufferval aMutableArr = ArrayBuffer(10,20,30)println(aMutableArr)aMutableArr += 40println(aMutableArr)aMutableArr.insert(2,60)println(aMutableArr)aMutableArr -= 40println(aMutableArr)}
元组
def main(args: Array[String]): Unit = {val tuple = ("Bigdata",2022,45.0)println(tuple)println(tuple._1)println(tuple._2)}
构造器
class Counter{private var value = 0 // value用来储存计数器的起始值private var name = "" //表示计数器的名称private var mode = 1 // mode用来表示计数器的类型(比如,1表示部署计数器,2表示时间计数器def this(name: String){ // 第一个辅助计数器this() // 调用主构造器this.name = name}def this(name: String,mode:Int){// 第二个辅助构造器this(name) // 调用前一个辅助构造器this.mode = mode}def increment(step: Int):Unit = {value += step}def current():Int = {value}def info():Unit = {printf("name:%s and mode is %d\n",name,mode)}}object Main {def main(args: Array[String]): Unit = {val myCounter = new Counter // 主构造器val counter1 = new Counter("Runner")val counter2 = new Counter("Timer",2)myCounter.info() // 显示计数器信息myCounter.increment(1) // 设置步长println(myCounter.current)counter1.info()counter1.increment(2)println(counter1.current)counter2.info()counter2.increment(3)println(counter2.current)}
}
- 构造器在参数中
class Counter(val name:String = "",val mode:Int = 1){private var value = 0 // value用来储存计数器的起始值def increment(step: Int):Unit = {value += step}def current():Int = {value}def info():Unit = {printf("name:%s and mode is %d\n",name,mode)}}object Main {def main(args: Array[String]): Unit = {val myCounter = new Counter // 主构造器val counter1 = new Counter("Runner")val counter2 = new Counter("Timer",2)myCounter.info() // 显示计数器信息myCounter.increment(1) // 设置步长println(myCounter.current)counter1.info()counter1.increment(2)println(counter1.current)counter2.info()counter2.increment(3)println(counter2.current)}
}
单例对象
Scala并没有提供Java那样的静态方法和静态字段,但是可以采用object关键字实现单例对象,具备和Java静态方法相同的功能。
object Person{private var lastId = 0 // 身份证号def nerPersonId()={lastId += 1lastId}
}
伴生类和伴生对象
class Person{private val id = Person.newPersonId() // 调用了伴生对象中的方法private var name = ""def this(name: String){this()this.name = name}def info() {printf("the id of %s is %d.\n",name,id)}
}object Person{private var lastId = 0 // 身份证号def newPersonId()={lastId += 1lastId}
}object Main {def main(args: Array[String]): Unit = {val person1 = new Person("ziyu")val person2 = new Person("Minxing")person1.info()person2.info()}
}
apply方法和update方法
- 我们经常会用到对象的apply方法和update方法,虽然我们表面上并没有察觉,但是实际上,在Scala中,apply方法和update方法都会遵循相关的约定被调用
- apply方法:用括号传递给变量(对象)一个或多个参数时,Scala会把它转换成对apply方法的调用
class TestApplyclass{def apply(param:String):String = {println(param)return "hello world"}
}object Main {def main(args: Array[String]): Unit = {val myObject = new TestApplyclassprintln(myObject("param1"))}
}
这种方式生成对象 会自动调用apply方法
class Main{}class ApplyTest{def apply() = println("apply method id class is called!")def greetingOfclass():Unit = {println("Greeting method in class is called.")}
}object ApplyTest{def apply() = {println("apply method in object is called")new ApplyTest() // 这行不会自动调用ApplyTest伴生类的apply方法}
}object Main {def main(args: Array[String]): Unit = {val a = ApplyTest() // 这里会调用伴生对象中的apply方法a.greetingOfclass()a() // 这里会调用半生类中的apply方法}
}
- update方法:当对带有括号并包括一到若干参数的对象进行赋值时,编译器将调用对象的update方法,在调用时,是把括号里的参数和等号右边的对象一起作为update方法的输入参数来执行调用
抽象类和继承
abstract class Car{val carBrand: Stringdef infodef greeting() {println("welcome to my car!")}
}
class BMWCar extends Car{override val carBrand: String = "BMW"def info(){println(this.carBrand)}override def greeting(){println("welcome to BMW car!")}}
特质
特质概述
- Java中提供了接口,允许一个类实现任意数量的接口
- 在Scala中没有接口的概念,二十提供了“特质(trait)”,它不仅实现了接口的功能,还具备了很多其他的特性
- Scala的特质,是代码重用的基本单元,可以同时拥有抽象方法和具体方法
- Scala中,一个类只能继承自一个超类,却可以实现多个特质,从而重用特质中的方法和字段,实现了多重继承
trait CarId{var id:Intdef currentId():Int //定义了一个抽象方法
}class BYDCarId extends CarId{override var id = 10000 // BYD汽车编号从10000开始def currentId():Int = {id += 1;id}
}
class BMWCarId extends CarId{override var id = 20000 // BMW汽车编号从20000开始def currentId():Int = {id += 1;id}
}