-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04_Classes_and_Properties.sc
714 lines (598 loc) · 16.3 KB
/
04_Classes_and_Properties.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// 4.1 Creating a Primary Constructor
class Person(var firstName: String, var lastName: String) {
println("the constructor begins")
// some class fields
private val HOME = System.getProperty("user.home")
var age = 0
// some methods
override def toString = s"$firstName $lastName is $age years old"
def printHome { println(s"HOME = $HOME") }
def printFullName { println(this) } // uses toString
printHome
printFullName
println("still in the constructor")
}
val p = new Person("Adam", "Meyer")
p.firstName = "Scott"
p.lastName = "Jones"
println(p.firstName)
println(p.lastName)
p.age = 30
println(p.age)
object Test extends App {
val p = new Person
// the 'normal' mutator approach
p.name = "Ron Artest"
println(p)
// the 'hidden' mutator method
p.name_$eq("Metta World Peace")
println(p)
}
// 4.2 Controlling the Visibility of Constructor Fields
class Person(var name: String)
val p = new Person("Alvin Alexander")
p.name
p.name = "Fred Flintstone"
p.name
class Person(val name: String)
val p = new Person("Alvin Alexander")
p.name
// default is very restricted
class Person(name: String)
val p = new Person("Alvin Alexander")
// p.name // => value name is not a member of ammonite.$sess.cmd228.Person
class Person(private var name: String) { def getName {println(name)} }
val p = new Person("Alvin Alexander")
// p.name // => variable name in class Person cannot be accessed in ammonite.$sess.cmd231.Person
p.getName
case class Person(name: String)
val p = Person("Dale Cooper")
p.name
// 4.3 Defining Auxiliary Constructors
// primary constructor
{
class Pizza(var crustSize: Int, var crustType: String) {
// one-arg auxiliary constructor
def this(crustSize: Int) {
this(crustSize, Pizza.DEFAULT_CRUST_TYPE)
}
// one-arg auxiliary constructor
def this(crustType: String) {
this(Pizza.DEFAULT_CRUST_SIZE, crustType)
}
// zero-arg auxiliary constructor
def this() {
this(Pizza.DEFAULT_CRUST_SIZE, Pizza.DEFAULT_CRUST_TYPE)
}
override def toString = s"A $crustSize inch pizza with a $crustType crust"
}
object Pizza {
val DEFAULT_CRUST_SIZE = 12
val DEFAULT_CRUST_TYPE = "THIN"
}
val p1 = new Pizza(Pizza.DEFAULT_CRUST_SIZE, Pizza.DEFAULT_CRUST_TYPE)
val p2 = new Pizza(Pizza.DEFAULT_CRUST_SIZE)
val p3 = new Pizza(Pizza.DEFAULT_CRUST_TYPE)
val p4 = new Pizza
}
// initial case class
case class Person (var name: String, var age: Int)
val p = Person("John Smith", 30)
val p = Person.apply("John Smith", 30)
{
// the case class
case class Person(var name: String, var age: Int)
// the companion object
object Person {
def apply() = new Person("<no name>", 0)
def apply(name: String) = new Person(name, 0)
}
object CaseClassTest extends App {
val a = Person() // corresponds to apply()
val b = Person("Pam") // corresponds to apply(name: String)
val c = Person("William Shatner", 82)
println(a)
println(b)
println(c)
// verify the setter methods work
a.name = "Leonard Nimoy"
a.age = 82
println(a)
}
CaseClassTest.main(Array())
}
// 4.4 Defining a Private Primary Constructor
class Person private (name: String)
// val p = new Person("Mercedes")
{
class Brain private {
override def toString = "This is the brain."
}
object Brain {
val brain = new Brain
def getInstance = brain
}
object SingletonTest extends App {
// this won't compile
// val brain = new Brain
// this works
val brain = Brain.getInstance
println(brain)
}
}
{
object FileUtils {
def readFile(filename: String) = {
// code here ...
}
def writeToFile(filename: String, contents: String) {
// code here ...
}
}
val contents = FileUtils.readFile("input.txt")
FileUtils.writeToFile("output.txt", content)
}
// 4.5 Providing Default Values for Constructor Parameters
{
class Socket(val timeout: Int = 10000)
val s = new Socket
s.timeout
val s = new Socket(5000)
s.timeout
val s = new Socket(timeout = 5000)
s.timeout
class Socket(val timeout: Int) {
def this() = this(10000)
override def toString = s"timeout: $timeout"
}
}
{
class Socket(val timeout: Int = 1000, val linger: Int = 2000) {
override def toString = s"timeout: $timeout, linger: $linger"
}
println(new Socket)
println(new Socket(3000))
println(new Socket(3000, 4000))
println(new Socket(timeout=3000, linger=4000))
println(new Socket(linger=4000, timeout=3000))
println(new Socket(timeout=3000))
println(new Socket(linger=4000))
}
// 4.6 Overriding Default Accessors and Mutators
{
class Person(private var _name: String) {
def name = _name // accessor
def name_=(aName: String) { _name = aName } // mutator
}
val p = new Person("Jonathan")
p.name = "Jony" // setter
println(p.name) // getter
}
// intentionally left the 'private' modifier off _symbol
class Stock(var _symbol: String) { // getter
def symbol = _symbol
// setter
def symbol_=(s: String) {
this.symbol = s
println(s"symbol was updated, new value is $symbol")
}
}
// 4.7 Preventing Getter and Setter Methods from Being Generated
class Stock {
// getter and setter methods are generated
var delayedPrice: Double = _
// keep this field hidden from other classes
private var currentPrice: Double = _
}
{
class Stock {
// a private field can be seen by any Stock instance
private var price: Double = _
def setPrice(p: Double) { price = p }
def isHigher(that: Stock): Boolean = this.price > that.price
}
object Driver extends App {
val s1 = new Stock
s1.setPrice(20)
val s2 = new Stock
s2.setPrice(100)
println(s2.isHigher(s1))
}
}
class Stock {
// a private[this] var is object-private, and can only be seen
// by the current instance
private[this] var price: Double = _
def setPrice(p: Double) { price = p }
// error: this method won't compile because price is now object-private
// => cmd262.sc:7: value price is not a member of ammonite.$sess.cmd262.Stock
def isHigher(that: Stock): Boolean = this.price > that.price
}
// 4.8 Assigning a Field to a Block or Function
{
class Foo {
// set 'text' equal to the result of the block of code
val text = {
var lines = ""
try {
lines = io.Source.fromFile("/etc/passwd").getLines.mkString
} catch {
case e: Exception => lines = "Error happened"
}
lines
}
println(text)
}
object Test extends App {
val f = new Foo
}
}
class Foo {
import scala.xml.XML
// assign the xml field to the result of the load method
val xml = XML.load("http://example.com/foo.xml")
// more code here ...
}
{
class Foo {
val text =
scala.io.Source.fromFile("/etc/passwd").getLines.foreach(println)
}
object Test extends App {
val f = new Foo
}
Test.main(Array())
}
{
class Foo {
lazy val text =
scala.io.Source.fromFile("/etc/passwd").getLines.foreach(println)
}
object Test extends App {
val f = new Foo
}
Test.main(Array())
}
{
case class Address(city: String, state: String, zip: String)
case class Person(var username: String, var password: String) {
var age = 0
var firstName = ""
var lastName = ""
var address = None: Option[Address]
}
val p = Person("alvinalexander", "secret")
p.address = Some(Address("Talkeetna", "AK", "99676"))
p.address.foreach { a =>
println(a.city)
println(a.state)
println(a.zip)
}
}
var i = 0 // Int
var d = 0.0 // Double
var b:Byte =0
var c:Char =0
var f: Float = 0
var l:Long =0
var s: Short = 0
{
class Person(var name: String, var address: Address) {
override def toString = if (address == null) name else s"$name @ $address"
}
case class Address(city: String, state: String)
// *var* age
class Employee(name: String, address: Address, var age: Int) extends Person(name, address) {
// rest of the class
}
val teresa = new Employee("Teresa", Address("Louisville", "KY"), 25)
teresa.name
teresa.address
teresa.age
}
// 4.11 Calling a Superclass Constructor
{
// (1) primary constructor
class Animal(var name: String, var age: Int) {
// (2) auxiliary constructor
def this(name: String) {
this(name, 0)
}
override def toString = s"$name is $age years old"
}
// calls the Animal one-arg constructor
class Dog(name: String) extends Animal(name) {
println("Dog constructor called")
}
// call the two-arg constructor
class Dog(name: String) extends Animal(name, 0) {
println("Dog constructor called")
}
}
{
case class Address(city: String, state: String)
case class Role(role: String)
class Person(var name: String, var address: Address) {
// no way for Employee auxiliary constructors to call this constructor
def this(name: String) {
this(name, null)
address = null
}
override def toString = if (address == null) name else s"$name @ $address"
}
class Employee(name: String, role: Role, address: Address) extends Person(name, address) {
def this(name: String) {
this(name, null, null)
}
def this(name: String, role: Role) {
this(name, role, null)
}
def this(name: String, address: Address) {
this(name, null, address)
}
}
}
// 4.12 When to Use an Abstract Class
// raits don’t allow constructor parameters:
// this won't compile
trait Animal(name: String)
abstract class Animal(name: String)
abstract class BaseController(db: Database) {
def save { db.save }
def update { db.update }
def delete { db.delete }
// abstract
def connect
// an abstract method that returns a String
def getStatus: String
// an abstract method that takes a parameter
def setServerName(serverName: String)
}
// 4.13 Defining Properties in an Abstract Base Class (or Trait)
{
abstract class Pet(name: String) {
val greeting: String
var age: Int
def sayHello { println(greeting) }
override def toString = s"I say $greeting, and I'm $age"
}
class Dog(name: String) extends Pet(name) {
val greeting = "Woof"
var age = 2
}
class Cat(name: String) extends Pet(name) {
val greeting = "Meow"
var age = 5
}
object AbstractFieldsDemo extends App {
val dog = new Dog("Fido")
val cat = new Cat("Morris")
dog.sayHello
cat.sayHello
println(dog)
println(cat)
// verify that the age can be changed
cat.age = 10
println(cat)
}
AbstractFieldsDemo.main(Array())
}
{
// abstract class def, concrete class val.
abstract class Pet(name: String) {
def greeting: String
}
class Dog(name: String) extends Pet(name) {
v
al greeting = "Woof"
}
object Test extends App {
val dog = new Dog("Fido")
println(dog.greeting)
}
}
{
abstract class Animal {
val greeting = "Hello"
def sayHello { println(greeting) }
def run
}
class Dog extends Animal {
override val greeting = "Woof"
def run {
println("Dog is running")
}
}
}
{
abstract class Animal {
val greeting = { println("Animal"); "Hello" }
}
class Dog extends Animal {
override val greeting = {
println("Dog"); "Woof"
}
}
object Test extends App {
new Dog
}
Test.main(Array())
}
{
abstract class Animal {
final val greeting = "Hello" // made the field 'final'
}
class Dog extends Animal {
val greeting = "Woof" // this line won't compile
}
}
{
abstract class Animal {
var greeting = "Hello"
var age = 0
override def toString = s"I say $greeting, and I'm $age years old."
}
class Dog extends Animal {
greeting = "Woof"
age = 2
}
}
{
trait Animal {
val greeting: Option[String]
var age: Option[Int] = None
override def toString = s"I say $greeting, and I'm $age years old."
}
class Dog extends Animal {
val greeting = Some("Woof")
age = Some(2)
}
object Test extends App {
val d = new Dog
println(d)
}
Test.main(Array())
}
// 4.14 Generating Boilerplate Code with Case Classes
{
case class Person(name: String, relation: String)
val emily = Person("Emily", "niece")
emily.name
emily.name = "Fred" // => error: reassignment to val
}
{
case class Company(var name: String)
val c = Company("Mat-Su Valley Programming")
c.name
c.name = "Valley Programming"
}
{
case class Person(name: String, relation: String)
val emily = Person("Emily", "niece")
emily match { case Person(n, r) => println(n, r) }
val hannah = Person("Hannah", "niece")
emily == hannah
}
{
case class Employee(name: String, loc: String, role: String)
val fred = Employee("Fred", "Anchorage", "Salesman")
val joe = fred.copy(name = "Joe", role = "Mechanic")
}
// 4.15 Defining an equals Method (Object Equality)
class Person(name: String, age: Int) {
def canEqual(a: Any) = a.isInstanceOf[Person]
override def equals(that: Any): Boolean = that match {
case that: Person => that.canEqual(this) && this.hashCode == that.hashCode
case _ => false
}
override def hashCode: Int = {
val prime = 31
var result = 1
result = prime * result + age;
result = prime * result + (if (name == null) 0 else name.hashCode)
result
}
}
// http://www.scalatest.org/install
import $ivy.`org.scalatest::scalatest:3.1.0`
import org.scalatest.{FunSuite, BeforeAndAfter}
{
class PersonTests extends FunSuite {
// these first two instances should be equal
val nimoy = new Person("Leonard Nimoy", 82)
val nimoy2 = new Person("Leonard Nimoy", 82)
val shatner = new Person("William Shatner", 82)
val ed = new Person("Ed Chigliak", 20)
// all tests pass
test("nimoy == nimoy") { assert(nimoy == nimoy) }
test("nimoy == nimoy2") { assert(nimoy == nimoy2) }
test("nimoy2 == nimoy") { assert(nimoy2 == nimoy) }
test("nimoy != shatner") { assert(nimoy != shatner) }
test("shatner != nimoy") { assert(shatner != nimoy) }
test("nimoy != null") { assert(nimoy != null) }
test("nimoy != String") { assert(nimoy != "Leonard Nimoy") }
test("nimoy != ed") { assert(nimoy != ed) }
}
org.scalatest.run(new PersonTests())
}
{
class Employee(name: String, age: Int, var role: String) extends Person(name, age) {
override def canEqual(a: Any) = a.isInstanceOf[Employee]
override def equals(that: Any): Boolean = that match {
case that: Employee => that.canEqual(this) && this.hashCode == that.hashCode
case _ => false
}
override def hashCode: Int = {
val ourHash = if (role == null) 0 else role.hashCode
super.hashCode + ourHash
}
}
class EmployeeTests extends FunSuite with BeforeAndAfter {
// these first two instance should be equal
val eNimoy1 = new Employee("Leonard Nimoy", 82, "Actor")
val eNimoy2 = new Employee("Leonard Nimoy", 82, "Actor")
val pNimoy = new Person("Leonard Nimoy", 82)
val eShatner = new Employee("William Shatner", 82, "Actor")
test("eNimoy1 == eNimoy1") { assert(eNimoy1 == eNimoy1) }
test("eNimoy1 == eNimoy2") { assert(eNimoy1 == eNimoy2) }
test("eNimoy2 == eNimoy1") { assert(eNimoy2 == eNimoy1) }
test("eNimoy != pNimoy") { assert(eNimoy1 != pNimoy) }
test("pNimoy != eNimoy") { assert(pNimoy != eNimoy1) }
}
org.scalatest.run(new PersonTests())
}
// 4.16 Creating Inner Classes
{
class PandorasBox {
case class Thing(name: String)
var things = new collection.mutable.ArrayBuffer[Thing]()
things += Thing("Evil Thing #1")
things += Thing("Evil Thing #2")
def addThing(name: String) { things += new Thing(name) }
}
object ClassInAClassExample extends App {
val p = new PandorasBox
p.addThing("Evil Thing #3")
p.addThing("Evil Thing #4")
p.things.foreach(println)
}
ClassInAClassExample.main(Array())
}
{
class OuterClass {
class InnerClass {
var x = 1
}
}
object ClassInObject extends App {
// inner classes are bound to the object
val oc1 = new OuterClass
val oc2 = new OuterClass
val ic1 = new oc1.InnerClass
val ic2 = new oc2.InnerClass
ic1.x = 10
ic2.x = 20
println(s"ic1.x = ${ic1.x}")
println(s"ic2.x = ${ic2.x}")
}
ClassInObject.main(Array())
}
{
object OuterObject {
class InnerClass {
var x = 1
}
}
class OuterClass {
object InnerObject {
val y = 2
}
}
object InnerClassDemo2 extends App {
// class inside object
println(new OuterObject.InnerClass().x)
// object inside class
println(new OuterClass().InnerObject.y)
}
InnerClassDemo2.main(Array())
}