class TestParent{
private void method1(){
System.out.println("parent's method1...");
    }
public void method2(){
System.out.println("Parent's methos2...");
method1();
}
}public class TestChild extends TestParent {
public void method1(){
System.out.println("child's method1...");
}
public static void main(String[] args){
TestParent p = new TestChild();
p.method2();

} }
为何是下面的输出:
Parent's methos2...
parent's method1...

解决方案 »

  1.   

    method1的访问修饰符是private的
      

  2.   

    因为 private void method1()的方法是private的,则method1方法被自动认为是final的。
    不能被导出类覆盖。
    所以,建议private 方法,父类子类中用不同的名称。
      

  3.   

    TestParent p = new TestChild(); //创建一个父类指向子类对象的引用p.method2(); //子类TestChild 没有继承父类TestParent的method2()方法,故调用父类TestParent的method2()方法,输出Parent's methos2... ,子类TestChild 并没有继承TestParent的method1()方法,并且TestParent的method1()方法也不能被继承,因为他是私有的,子类TestChild 的method1()是创建的一个新的方法,父类TestParent并没有这个方法,故不是被继承来的。当输出Parent's methos2... 后,父类TestParent又调用了自己的私有方法method1(),故输出parent's method1...。你 还有什么不明白的吗?
      

  4.   

    B是A的子类,A a = new B();当利用引用调用实例方法的时候,你要看引用所代表当前对象的类决定了执行哪一种方法的实现; 
    而调用字段或者静态方法或者final方法(或者private方法,private方法隐含的意思就是最终的,不能被覆盖的)的时候,你要看引用的类型(不是引用所代表当前对象的类)。 
    规定,记住就行了public class Test {
        public static void main(String []args){
            A a = new B();//a是A引用类型,代表的是类B
            B b = new B();
            System.out.println(a.ii);//打印出a自己的ii.(调用字段或者静态方法的时候,你要看引用的类型(不是引用所代表当前对象的类))
            System.out.println(b.ii);
            a.method1();//执行a自己的method1.(调用字段或者静态方法的时候,你要看引用的类型(不是引用所代表当前对象的类))
            b.method1();
            a.method2();//执行b的method2,method2是实例方法
            b.method2();
           a.method3();//执行a的method3,私有方法,你要看引用类型,不是引用所代表当前对象的类。
        }}
    class A{
        public int ii=1;
        public static void method1(){
            System.out.println("A:static method");
        }
        public void method2(){
            System.out.println("A:not static method");
        }
        private void method3(){
            System.out.println(A:private method);
        }
    }
    class B extends A{
        public int ii =2;
        public static void method1(){
            System.out.println("B:static method");
        }
        public void method2(){
            System.out.println("B:not static method");
        }
        private void method3(){
            System.out.println("B:private method");
        }}
      

  5.   

    饿,写错了,不能直接调用,改成这样
    在A类中增加个
    public void method4(){
         method3();
    }
    在main方法中将a.method3()改成a.method4()
      

  6.   

    很简单。父类中的method1声明为private的方法。所以在子类中的method1不是override了父类中的方法。而且,继承的特点是,子类不会集成父类中声明为private的方法的。所以,输出是那样的。TestParent p = new TestChild(); 
    一个父类的引用,调用的当然是父类的方法。
      

  7.   

    只有一条p.method2(); 
    真的会输出两条????还是有点不明白,为什么后面还要自动调自己的私有方法?
      

  8.   

    TestParent p = new TestChild(); //对象的多态性,子类对象向父类对象转型,调用的方法当然是父类中的方法了
      

  9.   

    这个
    很正常啊~class TestParent{                  
    private void method1(){      //虽然这个方法是private的 但下面给出了public的method2()相当于给出了外部访问接口
    System.out.println("parent's method1...");
        }
    public void method2(){
    System.out.println("Parent's methos2...");
    method1();                    //在public的method2()里是可以访问本类的private属性&方法
    }
    }public class TestChild extends TestParent {
    public void method1(){
    System.out.println("child's method1...");
    }
    public static void main(String[] args){
    TestParent p = new TestChild();
    p.method2();                   //调用继承来的method2()}}
    明白了么?我的语言表达能力很差
      

  10.   

    哦我眼神问题,没看到method2(){}里面还有个method1();。
      

  11.   

    TestChild的method1没有覆盖父类的method1,而是属于TestChild,相当于TestChild自己定义一个方法.
    如果写成这样,就成了动态绑定:
    class TestParent{
    public void method1(){
    System.out.println("parent's method1...");
        }
    public void method2(){
    System.out.println("Parent's methos2...");
    method1();
    }
    }public class TestChild extends TestParent {
    public void method1(){
    System.out.println("child's method1...");
    }
    public static void main(String[] args){
    TestParent p = new TestChild();
    p.method2();}
    }输出:
    Parent's methos2...
    child's method1...