作者PsMonkey (痞子军团团长)
看板java
标题Re: [问题] 禁止子类别宣告与上层变数名称相同的变数
时间Tue Mar 10 12:42:49 2009
※ 引述《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