父类:
package testonly;public class HelloFather {
  public HelloFather(){
    System.out.println("HelloFather.HelloFather()");
  }
  public void sayHello(){
    System.out.println("hello, world!");
  }
}子类:
package testonly;public class HelloSon extends HelloFather {
  public HelloSon() {
  }
  public void execute(){
      execute(super);           *******************
  }
  public void execute(HelloFather hf){
    hf.sayHello();
  }
  public static void main(String[] args) {
    HelloSon helloSon1 = new HelloSon();
    helloSon1.execute();
  }
}在eclispe下,这句编译不通过,强行运行也出错。是什么原因?super可以这么用吗?

解决方案 »

  1.   

    super不能这么用,你在这里想干什么?调用父类的构造方法吗?
      

  2.   

    不是,是将父类对象的引用传给execute函数
      

  3.   

    还有,如果在son中有以下语句:
       System.out.println("this : " + this.hashCode());
       System.out.println("super : " + super.hashCode());
      
       System.out.println("this : " + this.getClass());
       System.out.println("super : " + super.getClass());
    它们的结果是:
    this : 8187137
    super : 8187137
    this : class testonly.HelloSon
    super : class testonly.HelloSon
    也就是说,this与super指向同一个对象。可是,在子类重载了父类的某一方法时,super又的确可以调用父类中的方法。这是为什么?
      

  4.   

    在jbuilder2005下编译也不通过。
      

  5.   

    把SayHello()方法改为static就行了吧,因为调用它的时候你类没有实例化,成员方法是不能调用的
      

  6.   

    System.out.println("this : " + this.hashCode());
       System.out.println("super : " + super.hashCode());
      
       System.out.println("this : " + this.getClass());
       System.out.println("super : " + super.getClass());你把this和super去掉 你看一下,结果还是一样的 ! 
    这里所调用的都是HelloSon这个类的hashCode的值.
      

  7.   

    to bluet2001(bluet) :
    问题在这一句:
    execute(super);           *******************
    与sayHello()无关to  interhanchi(艰难困苦,玉汝于成!) :
    super.hashCode调用的是父类对象的hashCode()函数,如果去掉this和super,则调用的都是子类的hashCode()函数
      

  8.   

    super的意思,只是调用父类的方法,又不是等同于父类对象,如果能等同于父类对象那还得了!
      

  9.   

    经验证,只能在jb8的IDE环境下运行,在DOS下用jb8的jdk都不能通过编译。在jdk5.0下也不行。另:在jb8下,传super与传this的效果一样。(如果子类重载了父类的方法,以这种方式把super传过去后,调用的是子类的方法。
      

  10.   

    我一开始思考这个问题的时候,是把super和this做类比的。因为this是指向对象本身的引用,所以super有可能是指向父类对象的引用。而且,的确super拥有部分引用的功能,比如调用父类的函数、属性等,但是慢慢地发现,它又不能像this那样作为引用传给别的函数。 所以我就开始疑惑,到底super是什么? 生成了一个子类对象的时候,是不是有一个同时也生成了一个父类对象?还是父类对象作为一部分,被包含在子类对象中? 这个super,倒底是作为引用存在的,还是只是一个特殊的关键字,被赋予了少量的功能,由编译器来转化和实现。 为什么super.getClass()得到的是子类的类型等等? 我真的很想知道,我已经想了6个小时了,有没有人可以提示一下呢?
      

  11.   

    method(this)  // right
    method(super) // right ???
    一个对象,是存在它的引用的,但,存在它的父类对象的引用吗?我觉得没有。因为JAVA只创建了一个对象:this。在创建一个对象时没有创建它的父类的对象!所以个人认为你的程序是有问题的。
      

  12.   

    没见过用super做参数的 楼主大可不必在这个问题上较劲
      

  13.   

    ajoo给了一个满意的解答,有兴趣的朋友可以去看看http://www.javaeye.com/viewtopic.php?p=91155#91155