Given:
1. public class Base{
2. public static final String FOO = "foo";
3. public static void main(String[] args){
4. Base b = new Base();
5. Sub s = new Sub();
6. System.out.print(Base.FOO);
7. System.out.print(Sub.FOO);
8. System.out.print(b.FOO);
9. System.out.print(s.FOO);
10. System.out.print(((Base)s).FOO);
11. }
12. }
13. class Sub extends Base{public static final String FOO = "bar";}
What is the result?
A. foofoofoofoofoo
B. foobarfoobarbar
C. foobarfoofoofoo
D. foobarfoobarfoo
E. barbarbarbarbar
F. foofoofoobarbar
G. foofoofoobarfoo
答案:D
參考:6-5 繼承、8-1 變數種類與其生命期
-------------------------------------------
ANS:
6. System.out.print(Base.FOO); //output foo
7. System.out.print(Sub.FOO); //output bar
8. System.out.print(b.FOO); // output foo
9. System.out.print(s.FOO); //output bar
10. System.out.print(((Base)s).FOO); //output foo , s.FOO cast(轉型) to Base. s.FOO 是call 屬性 不是方法.
沒有留言:
張貼留言