Triangle tr=new Triangle();
  Shape sh=tr;这里Triangle是Shape的子类,那sh和tr是引用同一个对象吗,还是分别引用同一个对象的不同拷贝?还有,他们的内存空间是不是相同的?

解决方案 »

  1.   

    class Parent {
        int i = 3;    void test() {
            System.out.println("Parent test()");
        }
    }class Child extends Parent {
        int i = 5;    void test() {
            System.out.println("Child test()");
        }
    }public class HackersterTest {
        public static void main(String[] args) {
            Child c = new Child();
            System.out.println(c.i);// 输出5
            c.test();// 输出Child test()
            Parent p = c;
            System.out.println(p.i);// 输入3
            p.test();// 输出Child test()
        }
    }
    楼主看看这个例子