避免生成默认的setter getter

时间:2012-07-08 22:12:54

标签: scala

我将自己的getter和setter添加到变量中:

class Person{
    private var age = 0
    def currentAge = age
    def currentAge_=(age: Int) = this.age = age 
}

查看编译版本给出:

public class Person implements scala.ScalaObject {
    private int age;
    private int age();
    private void age_$eq(int);
    public int currentAge();
    public void currentAge_$eq(int);
    public Person();
}

我想避免自动生成默认的getter和setter。有可能吗?

1 个答案:

答案 0 :(得分:12)

private[this] var age = 0

以便age仅对实例可见。

相关问题