Scala面向对象

一、对象

说明:scala和java一样都有对象的概念,并且都用class来声明一个对象
声明方式:使用class关键字来声明对象

package obj

/**
 * @author aqi
 * @since 2023/7/11 9:37
 */
class Person {

  /**
   * 名字(使用_来设置默认值)
   */
  var name: String = _

  /**
   * 年龄(使用_来设置默认值)
   */
  var age: Int = _

  /**
   * 性别(设置初始值为0)
   */
  var gender = 0

  /**
   * 声明say方法
   * @param msg 入参
   */
  def say(msg: String): Unit = {
    println(msg)
  }

  /**
   * 重写toString方法
   */
  override def toString = s"Person($name, $age, $gender)"
}

二、构造器

说明:scala中分为主构造器和辅助构造器,但是我觉得主构造器得存在非常奇怪,辅助构造器其实已经满足了构造器得需要
声明方式:使用def this来声明

package obj

/**
 * @author aqi
 * @since 2023/7/11 9:37
 */

class Person {

  var name: String = _
  var age: Int = _
  var gender = 1

  def this(name: String, age: Int, gender: Int) = {
    this()
    this.name = name
    this.age = age
    this.gender = gender
  }

  def this(name: String, gender: Int) = {
    this()
    this.name = name
    this.gender = gender
  }

  def this(gender: Int) = {
    this()
    this.gender = gender
  }

  def this(name: String) = {
    this()
    this.name = name
  }

  /**
   * 声明say方法
   *
   * @param msg 入参
   */
  def say(msg: String): Unit = {
    println(msg)
  }

  /**
   * 重写toString方法
   */
  override def toString = s"Person($name, $age, $gender)"

}

三、伴生对象

说明:scala中的伴生对象类似于Java中的静态成员变量(static),用于在一个类中声明静态属性,主对象可以调用伴生对象中的所有属性和方法(即使是私有的),不过这种用法可能不多,一般对象中也不会去声明一个静态属性,如果是想调用一些静态属性可以直接声明一个object对象
声明方式:在相同的scala源文件中声明一个和类一样名称的被object修饰的对象就是半生对象

package obj

/**
 * @author aqi
 * @since 2023/7/11 9:37
 */

class Person {

  var name: String = _
  var age: Int = _
  var gender = 1

  /**
   * 声明say方法
   *
   * @param msg 入参
   */
  def say(msg: String): Unit = {
    // 调用半生对象中的私有属性
    println("调用伴生对象的MSG属性:" + Person.MSG)
    Person.write(name)
  }

  /**
   * 重写toString方法
   */
  override def toString = s"Person($name, $age, $gender)"
}

/**
 * 伴生对象
 */
object Person {
  private val MSG = "hello scala"

  private def write(name: String): Unit = {
    println(s"伴生对象write:$name")
  }
}

四、伴生对象apply方法

说明:伴生对象中的apply方法可以不使用new方法来初始化一个对象:val tom: Person = Person(“tom”)
声明方式:在伴生对象中定义apply方法,并初始化对象

package obj

/**
 * @author aqi
 * @since 2023/7/11 9:37
 */

class Person {

  var name: String = _
  var age: Int = _
  var gender = 1

  def this(name: String) = {
    this()
    this.name = name
  }

  /**
   * 重写toString方法
   */
  override def toString = s"Person($name, $age, $gender)"
}

object Person {

  def apply(name: String): Person = {
    new Person(name)
  }

}

五、继承

说明:scala中的继承和Java一样,可以在子类中定义父类中没有的字段和方法,或者重写父类的方法
声明方式:使用extends关键字来实现继承

package obj

/**
 * @author aqi
 * @since 2023/7/11 9:37
 */

class Person extends Game {
  var name: String = _
  var age: Int = _
  var gender = 1

  def this(name: String, gameName: String, playWay: String) = {
    this()
    this.name = name
    this.gameName = gameName
    this.playWay = playWay
  }

  /**
   * 重写父类的play方法
   */
  override def play(person: Person): Unit = {
    println("禁止玩游戏!!!!")
  }

}

class Game {
  var gameName: String = _
  var playWay: String = _

  def play(person: Person): Unit = {
    println(s"${person.name}正在使用${playWay}玩:$gameName")
  }

}

object testExtends{
  def main(args: Array[String]): Unit = {
    val person = new Person("tom", "潜水员戴夫", "steam")
    person.play(person)
  }
}

六、特质

说明:scala中没有接口(interface),所以如果想要实现多继承可以使用trait关键字,其用法和Java基本一致
声明方式:使用trait关键字来声明对象,并使用with来继承

package obj

/**
 * @author aqi
 * @since 2023/7/11 9:37
 */

class Person extends Food with Game with Happy{
  var name: String = _
  var age: Int = _
  var gender = 1

  override def play(person: Person): Unit = {
    println(s"${person.name}正在使用${playWay}玩:$gameName")
  }
}

trait Game {
  var gameName: String = _
  var playWay: String = _

  def play(person: Person): Unit
}

trait Food {
  var foodName: String = _
  var eatWay: String = _
}

trait Happy {
  var happyWay: String = _
}

trait Water {
  var water: String = _
}

object testTrait {
  def main(args: Array[String]): Unit = {
    val person = new Person with Water
    person.water = "芒果汁"
    println(person.water)
  }
}


你可能感兴趣的:(#,scala,scala,开发语言)