2015-06-10 67 views
2

我是一個java初學者。我的測試代碼:Java從另一個實例構造某個類的一個實例

public class Test { 
    private String a; 
    private String b; 

    public Test(String a, String b){ 
     this.a = a; 
     this.b = b; 
    } 

    public Test(Test another){ 
     this.a = another.a;//My question comes from here 
     this.b = another.b; 
    } 
    public String getA(){ 
     return a; 
    } 

    public String getB(){ 
     return b; 
    } 
} 

我的問題是,爲什麼Test another可以直接使用.,而不是使用getA()getB()方法訪問private variable

的教程告訴我,private variable不能直接通過.

+0

謝謝。我從你的鏈接中得到了答案。 – Gemsnail

回答

0

私有變量訪問不能由不同的類訪問,但在這裏,你是在同一個班級。

相關問題