前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spark学习使用笔记 - Scala篇(3)- 对象

Spark学习使用笔记 - Scala篇(3)- 对象

作者头像
干货满满张哈希
发布2021-04-12 16:21:16
3100
发布2021-04-12 16:21:16
举报
文章被收录于专栏:干货满满张哈希

field

代码语言:javascript
复制
class Counter {
  //field必须初始化,为了知道类型
  //会自动生成private的getter还有private的setter
  //setter和getter并不是getValue()和setValue()这样,而是value(),value_=()这样
  private var value = 0

  def increment() = {
    value += 1
  }

  def current() = value

  //类私有field可以访问
  def largerThan(a: Counter): Boolean = {
    value > a.value
  }

  //对象私有field别的对象无法访问
  private[this] var name = "test"

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

  //会自动生成public的getter和setter
  var times = 0
  //会自动生成public的getter
  val alloc = "hash"

}

object Counter1 {
  val counter = new Counter
  def testClass = {
    //习惯上取值器不加括号
    println(counter.current)//输出:0
    //习惯上改值器加括号
    counter.increment()
    println(counter.current)//输出:1

    val counter2 = new Counter
    println(counter.largerThan(counter2))//输出:true
  }
}

Constructor

代码语言:javascript
复制
//main constructor
class Person(val name: String, var age: Int, var salary: Int) {
  //主构造器会执行所有语句
  println("Main constructor is called!")

  //多态构造器
  def this(name: String, age: Int) {
    this(name, age, 0)
  }

  def description = "Name: " + name + ", Age: " + age + ", Salary: " + salary
}

object Person1 {
  def test = {
    val person = new Person("zhxhash", 24, 20000)//输出:Main constructor is called!
    println(person.description)//输出:Name: zhxhash, Age: 24, Salary: 20000

    val person2 = new Person("zhxdick", 15)//输出:Main constructor is called!
    println(person2.description)//输出:Name: zhxdick, Age: 15, Salary: 0
  }
}

练习:

代码语言:javascript
复制
object Exercise {

  class Counter(private var value: Int = 0) {
    def increment() = {
      if (value == Int.MaxValue)
        value = 0
      else
        value += 1
    }

    def current = value
  }

  def ex01 = {
    val counter = new Counter(Int.MaxValue)
    counter.increment()
    println(counter.current)//输出:0
  }

  class BankAccount(private var balance: Double) {
    def currentBalance = balance

    def deposit(value: Double) = {
      balance += value
    }

    def withdraw(value: Double) = {
      balance -= value
    }
  }

  def ex02 = {
    val bankAccount = new BankAccount(100000)
    bankAccount.deposit(99)
    bankAccount.withdraw(88)
    println(bankAccount.currentBalance)//输出:100011.0
  }

  class Time(private val hours: Int, private val minutes: Int) {
    if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59)
      throw new IllegalArgumentException()

    def before(other: Time): Boolean = {
      (hours > other.hours) || ((hours == other.hours) && (minutes > other.minutes))
    }

    def description = "[" + hours + ":" + minutes + "]"
  }

  def ex03 = {
    val time = new Time(13, 59)
    val time2 = new Time(13, 25)
    val time3 = new Time(14, 25)

    println(time.description + time2.description + time.before(time2))//输出:[13:59][13:25]true
    println(time.description + time3.description + time.before(time3))//输出:[13:59][14:25]false
  }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/07/31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • field
  • Constructor
  • 练习:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档