public Sub (String text){
i=2;
}
//i未定义

解决方案 »

  1.   

    当子类的构造器中未显式调用super(...)货this(...)时
    在第一条语句自动调用super(),而super无此构造器,当然出错了
      

  2.   

    class Super{ 
    public int i=0;
    public  Super(String text){
    i=1;
    }
    public Super(){i=1;}
    }
    public class  Sub extends Super

    public Sub(){i=2;}
    public Sub (String text){
    i=2;
    }
    public static void main(String args[]){
    Sub sub = new Sub ("hello");
    System.out.println(sub.i);
    }
    }
      

  3.   

    class Super{ 
    public int i=0;
    public  Super(){} //<----------------add code here
    public  Super(String text){
    i=1;
    }
    }
    public class  Sub extends Super

    public Sub (String text){
    i=2;
    }
    public static void main(String args[]){
    Sub sub = new Sub ("hello");
    System.out.println(sub.i);
    }
    }
      

  4.   

    搞错搞错,呵呵,不好意思
    class Super

      public int i=0;
      
      public Super(String text)
      {
        i=1;
      }
    }
    public class Sub extends Super

      public Sub(String text)
      {
        super(text);
        i=2;
      }
      
      public static void main(String args[])
      {
        Sub sub = new Sub ("hello");
        System.out.println(sub.i);
      }
    }
    搞定了,子类的构造器一定要显式调用super(参数表),否则系统会默认提供一个super();
      

  5.   

    还要请教一下,
    在class Sub 里如何实现打印i=2.
      

  6.   

    add super(text) on the first line of the Sub Construct.