-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApplyMethodDemo2.scala
37 lines (26 loc) · 758 Bytes
/
ApplyMethodDemo2.scala
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
package ScalaBasic
// https://blog.csdn.net/weixin_34358365/article/details/90627000
// Companion class and its class can visit the private object each other
object ApplyMethodDemo2 extends App {
println("*** ApplyMethodDemo2 run ...")
// Companion object can use its own private value
val p = new Person
println(p.age)
p.age = 10
println(Person.double(p))
println(p.printObj)
}
class Person {
private var _age = 2
//def age_(num : Int) = this._age = num
def age_=(num: Int) = this._age = num
def age = _age
def printObj: Unit = {
print(s"People can see ${Person.obj}")
}
}
object Person {
// access the private class field "age"
def double(p: Person) = p._age * 2
private val obj = "Object Persion's Object"
}