我在<JAVA大学教程>上看到的:
class Point {
   int x;
   int y;
   .....
   public String toString() {
       return "[" + x +"," + y + "]";
   }
}class Circle extends Point {
   int r;
   .....
   public String toString() {
      return "Center = " + super.toString() + "; Radius = " + radius;
  }
}
说明:
当子类覆盖超类的方法时,使用子类版本调用超类版本, 并进行一些附加的工作,这事很常见的.不要使用super引用来引用超类的方法,这样做会导致无限递归,因为子类方法实际上时在调用自己.这段话如何理解? 怎么是无限递归了呢???

解决方案 »

  1.   

    public String toString() {
          return "Center = " + toString() + "; Radius = " + radius;
      }
    或者
    class Point {
       int x;
       int y;
       public String toString(){
           return "[" + x +"," + y + "]";
       }
       public String toString(Point p){return p.toString();}
    }public class Circle extends Point {
       int r;
       public String toString() {
          return "Center = " + super.toString(this) + "; Radius = " + r;
      }
    }
    这样才是无限递归.
    super只会调用父类的执行体,this才能调用本类的执行体.
      

  2.   

    ^_^ .终于搞明白了..是中文翻译的有问题.哈哈
    原文如下:When an overridden method calls the superclass version of the same method, not using key-
    word super to reference the superclass’s method causes infinite recursion, because the
    subclass method actually calls itself.当子类覆盖超类的方法时,使用子类版本调用超类版本, 未使用super引用来引用超类的方法,这样做会导致无限递归,因为子类方法实际上时在调用自己.