0

我有一个具有受保护属性高度的子(扩展)类。我想在主程序中访问它:

   while(line != null)
     {
        String[] field = line.split("#");
        int height = Integer.parseInt(field[0]);

        if (field.length ==1)
        {
           forest[cnt] = new Trees(height);
        }
        else
        {
           forest[cnt] = new Shrub(height, field[1]);

        }

        cnt++;
        line = inS.readLine();

     }
     inS.close();

     String s = JOptionPane.showInputDialog("Enter Name to search for");

     for(int i = 0; i<forest.length; i++)
     {

        if (forest[i] instanceof Shrub)
        {
           String a = forest[i].getName();      
           System.out.println ("Found");
        }
     }
  }

但是我收到一个错误,说它找不到方法 getName,但是当我运行 lol Shrub 时它工作正常吗?

谢谢。

4

1 回答 1

0

私有访问修饰符方法在子类中不可访问。将它们设为公开或受保护。

于 2012-11-10T17:28:55.677 回答