private修飾的成員變量能被重新賦值嗎?
public?class?Study{
?private?int?a=0;
?public?void?setA{
??a=1;
?}
?public?int?getA{
?return?a;
?}
?public?static?void?main(String[]?args){
??Study?hello=new?Study();
??System.out.println("a:"+hello.a);
?}
}
輸出結(jié)果是a:0
這么說利用方法也不能改變private修飾的成員變量嗎?
望解答,謝謝。
2016-01-03
public?class?Student{ ?private?int?a=0; ?public?void?setA(){ ??a=1; ?} ?public?int?getA(){ ?return?a; ?} ?public?static?void?main(String[]?args){ ?Student?hello=new?Student(); ?hello.setA();?//需要調(diào)用Student對象的setA方法。 ?System.out.println("a:"+hello.a);??//輸出結(jié)果:?a:1 ?} }private 只是將成員變量私有化,不能直接的去修改private 修飾的成員變量。而需要通過調(diào)用get/set 方法去對其進行修改。
2016-01-11
方法沒參數(shù)
2016-01-02
setA方法你沒有調(diào)用為a賦值,當然你的hello.a=0。