一下有三组程序:
第一组:class Father{
private int a = 1;
private void method(){
System.out.println("private father method");
}
public static void main(String[] args) {
Father t = new Son();
// Son s0 = new Son();
// t=(Father)s0;
t.method();
System.out.println(t.a);
//////////////////////////////
Son s = new Son();
Father t2 = new Father();
t2 = (Father)s;
t2.method();
System.out.println(t2.a);
/////////////////////////////
System.out.println("-----------------");
Father t3 = (Father)s;
t3.method();
System.out.println(t3.a);
////////////////////////////////
System.out.println("-----------------");
Father t4 = new Son();
Father t5 = new Father();
t5=(Father)t4;
System.out.println(t5.a);
t5.method();
}
}
class Son extends Father{
public int a = 100;
public void method(){
System.out.println("public Son thod");
}
}
第二组:
class Father{
private int a = 1;
private void method(){
System.out.println("private father method");
}
}
class Son extends Father{
public int a = 100;
public void method(){
System.out.println("public Son thod");
}
public static void main(String[] args) {
Father t = new Son();
// Son s0 = new Son();
// t=(Father)s0;
t.method();
System.out.println(t.a);
//////////////////////////////
Son s = new Son();
Father t2 = new Father();
t2 = (Father)s;
t2.method();
System.out.println(t2.a);
/////////////////////////////
System.out.println("-----------------");
Father t3 = (Father)s;
t3.method();
System.out.println(t3.a);
////////////////////////////////
System.out.println("-----------------");
Father t4 = new Son();
Father t5 = new Father();
t5=(Father)t4;
System.out.println(t5.a);
t5.method();
}
}
第三组:class Father{
private int a = 1;
private void method(){
System.out.println("private father method");
}
}
class Son extends Father{
public int a = 100;
public void method(){
System.out.println("public Son thod");
}
}public class Test3 {
public static void main(String[] args) {
Father t = new Son();
// Son s0 = new Son();
// t=(Father)s0;
t.method();
System.out.println(t.a);
//////////////////////////////
Son s = new Son();
Father t2 = new Father();
t2 = (Father)s;
t2.method();
System.out.println(t2.a);
/////////////////////////////
System.out.println("-----------------");
Father t3 = (Father)s;
t3.method();
System.out.println(t3.a);
////////////////////////////////
System.out.println("-----------------");
Father t4 = new Son();
Father t5 = new Father();
t5=(Father)t4;
System.out.println(t5.a);
t5.method();
}
}
这三组程序,只是main方法的位置不同,第一组main方法位于父类,第二组位于子类,第三组位于另外的一个类,main方法完全一样,但是只有第一组程序编译通过,而且结果与预想的有区别,拜请大神们给解释一下,谢谢;

解决方案 »

  1.   

    第一组:
    private的方法不能继承,所以就不能重写;对于成员变量的值,就是看引用的类型:
    Father t3 = (Father) s;
    那么t3.a调用的是Father中的a,因为声明类型是Father
    第二组:
    父类Father中的method和a是私有的,在子类中看不到,所以不能调用
    第三组:
    基本同第二组,是因为在Test3看不到Father的私有成员
      

  2.   

    第二组Father的method是private的,son的method方法就得改名了,不能override;第三组不仅method是private的,错误同第二组,而且你的a属性也是private的,怎么能在Test3的main方法中用对象名直接调用呢。第一组没有语法错误,入口方法为Father的main方法。
      

  3.   

    回复二楼:第一组:Father中的method是private,在子类中也可以定义method方法,此处确实不是覆盖, 但也不会出错,因为method只是子类的一个成员方法而已,当然,不推荐这样做;
    我现在基本明白了,在第一组中之所以:Father t = new Son(); t.method();此处t调用的是父类的私有方法,此处之所以不会报错,是因为编译器把他看做是Father类内部的调用,就跟类中的一个方法可以调用本类的私有方法一样;