ITPub博客

首页 > 数据库 > 数据库开发技术 > Scala快速入门-6-单例对象及伴生对象

Scala快速入门-6-单例对象及伴生对象

原创 数据库开发技术 作者:HelloTech技术派 时间:2018-01-05 08:00:00 0 删除 编辑

知识点

  • 用对象作为单例或存放工具方法,Scala没有静态方法或字段

  • 类可以有一个同名的伴生对象

  • 对象的apply方法通常用来构造伴生类的新实例

  • Scala的main函数定义

单例对象

  • Scala没有静态方法或字段,可以用object语法定义结构,对象定义了类的单个实例。

  • 对象的构造器在该对象第一次使用时被调用。

  • 不能提供构造器参数。

  • 作为存放工具函数或常量的地方。

  • 高效地共享单个不可变实例。

  1. scala> object Accounts {

  2.     | private var lastNumber = 0

  3.     |

  4.     | def newUniqueNumber() = {

  5.     | lastNumber += 1

  6.     | lastNumber

  7.     | }

  8.     |

  9.     | }

  10. defined module Accounts

  11. scala>

  12. scala> Accounts.newUniqueNumber()

  13. res2: Int = 1

  14. scala> Accounts.newUniqueNumber()

  15. res3: Int = 2

伴生对象

  • 在Scala中,可通过类和类同名的“伴生”对象来达到静态方法的目的。

  • 类和它的伴生对象可以相互访问私有特性,它们必须存在于同一个源文件中

apply方法

  • 一般在伴生对象中定义apply方法

  • 常用于初始化操作或创建单例对象

  • 在生成这个类的对象时,就省去了new关键字

  • 在遇到Object(参数1,参数2,......,参数n)时就会自动调用apply()方法

main函数

  • Scala程序必须从一个对象的main方法开始

  • 有两种方法定义

  1. // 执行println语句

  2. object Main {

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

  4.    println("=" * 10)

  5.  }

  6. }

  7. // 扩展App特质

  8. object Main extends App {

  9.  println("=" * 10)

  10. }

完事示例

  1. class Account {

  2.  val id = Account.newUniqueNumber()

  3.  private var balance = 0.0

  4.  def deposit(amount: Double): Double = {

  5.    balance += amount

  6.    balance

  7.  }

  8.  def nowBalance = balance;

  9. }

  10. object Account {

  11.  private var lastNumber = 0

  12.  private def newUniqueNumber() = {

  13.    lastNumber += 1

  14.    lastNumber

  15.  }

  16. }

  17. object Main {

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

  19.    val account = new Account

  20.    println(account.id)

  21.    println(account.deposit(1))

  22.    println("=" * 10)

  23.    val account1 = new Account

  24.    println(account1.id)

  25.    println(account1.deposit(10))

  26.    println("=" * 10)

  27.    println("a " + account.nowBalance + "; b " + account1.nowBalance)

  28.  }

  29. }

  30. // ======= 执行结果

  31. 1

  32. 1.0

  33. ==========

  34. 2

  35. 10.0

  36. ==========

  37. a 1.0; b 10.0

  38. // =================使用apply示例===================

  39. package com.gemantic.bigdata

  40. /**

  41.  * @author Yezhiwei

  42.  * @date 18/1/4

  43.  */

  44. class Student private (val sno: Int, val name: String){

  45.  override def toString: String = {

  46.    "sno " + sno + " name " + name

  47.  }

  48. }

  49. object Student {

  50.  private var sno = 0

  51.  private def newSno = {

  52.    sno += 1

  53.    sno

  54.  }

  55.  def apply(name: String): Student = {

  56.    println("call apply method...")

  57.    new Student(newSno, name)

  58.  }

  59. }

  60. object StudentMain extends App {

  61.  // no new

  62.  val student1 = Student("Yezhiwei")

  63.  println(student1.toString)

  64.  println("*" * 10)

  65.  val student2 = Student("Yezhiwei")

  66.  println(student2.toString)

  67. }

  68. // 运行结果

  69. call apply method...

  70. sno 1 name Yezhiwei

  71. **********

  72. call apply method...

  73. sno 2 name Yezhiwei


敬请期待下一篇~


原文作者:HelloTech技术派;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2017-12-05

  • 博文量
    181
  • 访问量
    73725