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