2012-07-08 78 views
7

我說我自己的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和二傳手。可能嗎?

回答

12
private[this] var age = 0 

這樣age只對實例可見。