class student
{
    static int num;
    String name;
    int id;
  
    public  student()
    {
         num++;
         name="none";
         id=0;
     }
     
     public student(String sn,int snum)
     {
          num++;
          name=sn;
          id=snum;
     }
     
}
class grastu extends student
{
     String graschool;
     
     public grastu()
     {
    super();
     }
      
     public grastu(String sn,int snum,String sch)
     {
             super(sn,snum);
         this.graschool=sch;
     }     public  String getscl()
     {
             return graschool;
     }     
}class manstu
{
     public static void main(String []args)
     {
             student stu3=new grastu("liming",20,"nju");
    System.out.println(stu3.getscl());
}
}编译的时候总是说stu3.getscl()  cannot resolve symbol

解决方案 »

  1.   

    student 类里没有定义 getscl 方法,虽然 grastu 里有这个方法,但你的stu3已经被转换成student类型了。可以在student里写应该getscl方法,到运行是,会自动调用grastu里的getscl
      

  2.   

    唉,打了好几个错字,再来一次student 类里没有定义 getscl 方法,虽然 grastu 里有这个方法,但你的stu3已经被转换成student类型了。可以在student里写一个getscl方法,到运行时,会自动调用grastu里的getscl
      

  3.   

    student stu3=new grastu("liming",20,"nju");
    System.out.println(stu3.getscl());
    //你这个是生成一个子类对象,由父类引用(stu3)来指向,那么stu3所能操作的方法,也就只能操作父类中有的方法,getscl()属于子类的方法对于stu3是不可见的,所以编译器会报错;
    如下修改就可以:
    grastu stu3=new grastu("liming",20,"nju");
    System.out.println(stu3.getscl());
      

  4.   

    1、student类中没有getscl()方法
    2、将student stu3=new grastu("liming",20,"nju");
    ----------------------------------------------------
    改为:
    grastu stu3=new grastu("liming",20,"nju");
    就好了
      

  5.   

    System.out.println(stu3.getscl());改为System.out.println(((grastu)stu3).getscl());就OK了。你基类里没有定义getscl(),向上转型之后,不能自动调用派生类这个函数