Given:
1. class TestA {
2. public void start() { System.out.println(”TestA”); }
3. }
4. public class TestB extends TestA {
5. public void start() { System.out.println(”TestB”); }
6. public static void main(String[] args) {
7. ((TestA)new TestB()).start();
8. }
9. }
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B
why???????

解决方案 »

  1.   

    不就是static得用法么
    靜態加載
    好什么呀
      

  2.   


    楼上貌似牛头不对马嘴……main中的实体还是B,不管你怎么转
      

  3.   


    语言表达得好有意思~呵呵~
    ((TestA)new TestB()).start(); //这个理解成TestA t = new TestB();
    t.start(); 这不就是多态么~呵呵~动态加载!
      

  4.   

    ((TestA)new TestB()).start(); 不管你怎么转换类型,它调用的都是testB的start方法。 呵呵,输出内容除了报错就是B。