Spark学习使用笔记 - Scala篇(3)- 对象
来源:互联网 发布:cpu跑分软件 编辑:程序博客网 时间:2024/05/27 14:14
field
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
//main constructorclass 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 }}
练习:
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 }}
0 0
- Spark学习使用笔记 - Scala篇(3)- 对象
- Spark学习使用笔记 - Scala篇(4)- 对象、包
- Spark学习使用笔记 - Scala篇(1)
- Spark学习使用笔记 - Scala篇(2)- 数组
- Spark学习使用笔记 - Scala篇(5)- 继承
- Spark学习使用笔记 - Scala篇(3)- 映射,元组
- Spark学习笔记2-Scala的类,对象等
- scala 学习笔记--对象
- scala学习笔记-对象
- spark高级数据分析-scala学习(学习笔记)
- Scala学习笔记(3): 面向对象与容器
- Scala学习笔记3 - 类和对象
- Scala学习笔记(二)Scala面向对象编程
- Spark学习笔记3-Scala函数式编程
- Scala学习笔记(3)-Scala特质
- spark上的scala学习笔记
- spark学习笔记一:scala语言基础
- Spark学习笔记1-Scala基础语法
- MySQL|RESTRICT|NO ACTION|CASCADE| SET NULL四种外键约束
- 重建二叉树
- 内排序-插入类排序-希尔排序
- redis源码分析-ziplist(压缩链表)
- STM32 FATFAS的学习
- Spark学习使用笔记 - Scala篇(3)- 对象
- 【Cocos2d-x游戏开发】3.6 Win7打安卓包
- 从Windows系统服务获取活动用户的注册表信息
- hdu 1226超级密码
- Web开发中基础常识详解-Js,JSP,tomcat,Servlet,web是干什么的?
- 集合详解(四)----HashSet和HashMap源码剖析(JDK1.7)
- Android资源解析之mipmap
- BestCoder Round #85 抽屉原理/贪心/质因数
- 类别(Category)的作用(零)