object PrivateThisTest {
def main(args:Array[String]){
val c1 = new Counter
val c2= new Counter
c2.increment()
println(c1.isLess(c2))
}
}this
class Counter{
private var value =0
def increment() {
value += 1
}
def isLess(other:Counter):Boolean = {
value < other.value
}
}scala
打印结果:true对象
当class Counter代码修改以下:rem
class Counter{
private var value =0
def increment() {
value += 1
}
def isLess(other:Counter):Boolean = {
value < other.value //此句输入时IDE会提示出错
}
}get
说明当一个成员变量加上private[this]这个修饰符以后,在本类中能够访问这个变量。在本类的方法中不能访问同一类的其它对象的这个字段,也就形象理解为对象私有的。对于这种对象私有变量,scala根本不会生成getter和setterclass