原创文章,转载请注明出处!

原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-05/

There I am,in the world more exciting!

by postbird

www.ptbird.cn


05类的私有属性和this


 
package com.ptbird.scala /** * Created by postbird on 2016/5/25. */ class Person { private var age = 0 private[this] var privateAge = 0 //这里的区别在于 写了个这个之后 后面不需要使用this. 就可以直接访问这个私有元素 //限定这个成员只能归当前类所有 var name = "admin" def ageSetter(age2: Int){ this.age = age2} def isYounger(obj: Person) = this.age < obj.age } object ClassTest { def main(args: Array[String]): Unit = { var person1 = new Person() var person2 = new Person() person1.ageSetter(19) person2.ageSetter(18) println(person1.isYounger(person2)) //false } }