使用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.ArrayBuffer
val aMutableArr = ArrayBuffer(10,20,30)
println(aMutableArr)
aMutableArr += 40
println(aMutableArr)
aMutableArr.insert(2,60)
println(aMutableArr)
aMutableArr -= 40
println(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 += 1
lastId
}
}
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 += 1
lastId
}
}
object Main {
def main(args: Array[String]): Unit = {
val person1 = new Person("ziyu")
val person2 = new Person("Minxing")
person1.info()
person2.info()
}
}
class TestApplyclass{
def apply(param:String):String = {
println(param)
return "hello world"
}
}
object Main {
def main(args: Array[String]): Unit = {
val myObject = new TestApplyclass
println(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方法
}
}
abstract class Car{
val carBrand: String
def info
def 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!")}
}
特质概述
trait CarId{
var id:Int
def 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}
}