JIANGzihao0222

导航

 

  学习了Scala语言的面向对象方面的知识

类的定义

class Counter{
  private var value = 0
  def increment() : Unit = {
    value += 1
  }
   def current() : Int = {
     value
   }
}

若只有一行语句,可以写成

class Counter{
  private var value = 0
  def increment() : Unit = value += 1
  
   def current() : Int = {
     value
   }
}

若返回值为Unit,也可以这样进行

class Counter{
  private var value = 0
  def increment(){
    value += 1
  }

   def current() : Int = {
     value
   }
}

创建对象

val myCount = new Counter()
myCount.increment()
println(myCount.current())

编译和执行

进行编译时,先编译

scalac TestCounterJVM.scala

再运行

scala -classpath . Mycounter

注意:TestCounterJVM.scala为文件名,Mycounter为包含main方法的对象名称,object类

执行方法时进行传参

def increment(step : Int){
  value += step
}

getter、setter方法

通过自己定义的方法进行使用

value、value_

class Counter{
  private var privatevalue = 0

//  getter
  def value = privatevalue
//  setter
  def value_=(newValue : Int): Unit ={
    privatevalue = newValue
  }
  def increment(step : Int){
    privatevalue += step
  }

   def current() : Int = {
     privatevalue
   }


}

进行调用

def main(args: Array[String]): Unit = {

  val myCount = new Counter()
  println(myCount.value)
  myCount.value = 3
  println(myCount.value)

}

辅助构造器

通过以下的形式进行调用

 // 第一个辅助构造器
  def this(name : String){
    // 调用主构造器
    this()
    this.name = name
  }

  //第二个构造器
  def this(name : String,mode: Int) {
    // 调用主构造器
    this(name)
    this.mode = mode
  }

主构造器

将参数直接写到类后,进行配置,此时,不用进行name和mode字段的定义,在编译完成后会自动进行变成内部的字段

class Counter(name : String,mode : Int){
  private var privatevalue = 0
  
  def value_=(newValue : Int): Unit ={
    privatevalue = newValue
  }
  def increment(step : Int){
    privatevalue += step
  }

   def current() : Int = {
     privatevalue
   }

  def info():Unit = {
    printf("名称:%s,模式:%d",name,mode)
  }


}

对象:单例对象和伴生对象

scala不提供static的静态方法,使用object实现单例对象,定义在单例对象的字段相当于静态的

object HelloWorld {

  private var lastId = 0

  def newPersonId(): Int ={
      lastId += 1
       lastId
  }

  /*
  main方法
   */
  def main(args: Array[String]): Unit = {
    printf("The first person is %d\n",HelloWorld.newPersonId())
    printf("The Second person is %d\n",HelloWorld.newPersonId())
    printf("The third person is %d\n",HelloWorld.newPersonId())
  }

}

伴生对象:有类又有对象

在同一文件中,可以相互访问彼此的私有成员,但是伴生类和伴生对象名称必须相同,伴生类调用伴生对象(静态)直接调用即可,在编译后,合二为一,object成员成立static,class成员成立实例对象

应用程序对象

object HelloWorld {
  
  /*
  main方法
   */

  def main(args: Array[String]): Unit = {
    
    println("Hello World")
    
  }
  
}

运行时:

scalac 文件名.scala

scala -classpath . 应用程序对象名称

apply方法和update方法

apply:用圆括号传递一个对象一个或多个参数,Scala会把它转换成对Apply方法的调用、

注:调用伴生类的apply方法,必须通过实例进行启动,就是有new

update方法:当带有圆括号并包括一到多个参数进行赋值时,会调用对象的update方法,来执行调用

继承

重写非抽象方法必须使用override关键字

可以重写超类字段

只有主构造器可以调用超类的主构造器

定义一个抽象类:

abstract class Car {
  // 无初始值 抽象字段
  val carBrand : String
  // 抽象方法 后面无任何方法体
  def info()
  //具体方法
  def greeting(): Unit ={
    println("Welcome to my car!")
  }
}

子类:

class BMWCar extends Car {
  // 重写抽象字段
  override val carBrand = "BWM"
  // 重写抽象方法,对于重写超类的抽象方法,override可写可不写
  override def info(): Unit = {
    printf("This is a %s car.It is expensive\n",carBrand)
  }
  // 重写超类的具体方法,必须加override修饰符
  override def greeting(): Unit = {
    println("Welcome to my BWM car!")
  }

}
class BYDCar extends Car {

  override val carBrand = "BYD"

  // 重写抽象方法,对于重写超类的抽象方法,override可写可不写
  override def info(): Unit = {
    printf("This is a %s car.It is cheap\n", carBrand)
  }

  // 重写超类的具体方法,必须加override修饰符
  override def greeting(): Unit = {
    println("Welcome to my BYD car!")
  }


}

启动:

object MyCar {

  def main(args: Array[String]): Unit = {

    val myCar1 = new BMWCar()

    val myCar2 = new BYDCar()

    myCar1.greeting()
    myCar1.info()

    myCar2.greeting()
    myCar2.info()

  }
}

特质

scala里没有接口的概念,而是提供了特质

定义:

trait CarId {
  var id : Int
    def currentId() : Int
}

注意:在特质里定义抽象不需要 abstrat关键字,里没有方法体的方法会自动认为抽象方法

子类实现:

class BMWCarId extends CarId {
  override var id: Int = 20000

  override def currentId(): Int = {
    id += 1
    id
  }
}
class BYDCarId extends CarId {
  override var id: Int = 10000

  override def currentId(): Int = {
    id += 1
    id
  }
}

启动:

object MyCar {

  def main(args: Array[String]): Unit = {
    val myCarId1 = new BMWCarId()
    val myCarId2 = new BYDCarId()

    printf("My first CarId is %d\n",myCarId1.currentId())
    printf("My second CarId is %d\n",myCarId2.currentId())
  }

}

可以使用with进行实行多个特质, XXX extends XXX1 with XXX2 with XXX3 ...

例如:

特质:

trait CarGreeting {
  def greeting(msg : String): Unit ={
    println(msg)
  }
}

trait CarId {
  var id : Int
    def currentId() : Int
}

实现

class BYDCarId extends CarId with CarGreeting {
  override var id: Int = 10000

  override def currentId(): Int = {
    id += 1
    id
  }
}

class BMWCarId extends CarId with CarGreeting {
  override var id: Int = 20000

  override def currentId(): Int = {
    id += 1
    id
  }
}

启动:

object MyCar {

  def main(args: Array[String]): Unit = {
    val myCarId1 = new BMWCarId()
    val myCarId2 = new BYDCarId()

    myCarId1.greeting("Welcome my first car")
    printf("My first CarId is %d\n",myCarId1.currentId())
    myCarId1.greeting("Welcome my second car")
    printf("My second CarId is %d\n",myCarId2.currentId())
  }

}

 

posted on 2024-01-19 17:57  实名吓我一跳  阅读(17)  评论(0)    收藏  举报