Re: [問題] 禁止子類別宣告與上層變數名稱相同的變數
※ 引述《kenl (ken)》之銘言:
: 例如:
: class A{
: int i;
: class B{
: int i; // 這行的i會在class B的scope中覆蓋class A中的 i
: }
: }
: 我問題是能不能在class A宣告 int i 時
: 就限制底層不能把它覆蓋 ?
: 我用protected好像不行...
覆蓋? 我實在很想說:「請定義一下『覆蓋』的意思」
不過,這好像有點瞎...
還是來寫程式吧...
public class Test {
int a;
Foo foo;
class Foo{
int a;
Foo(){
this.a=100;
}
public String toString(){
return ""+a;
}
}
public String toString(){
return "this.a="+a + "\n foo.a="+foo.a + "\n "+foo;
}
Test(){
foo = new Foo();
this.a=-1;
}
public static void main(String[] args) {
Test a = new Test();
System.out.println(a);
}
}
當然,如果你只是要別人不能取同樣的名字...
ㄜ... 阿.... Orz
--
侃侃長論鮮窒礙 首頁:http://www.psmonkey.idv.tw
眾目睽睽無心顫 Blog:http://ps-think.blogspot.com
煢居少聊常人事
殺頭容易告白難 歡迎參觀 Java 版(@ptt.cc)精華區 \囧/
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 58.114.200.219
討論串 (同標題文章)