从超类的数组列表访问子类方法 [英] Accessing subclass method from arraylist of superclass

查看:24
本文介绍了从超类的数组列表访问子类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于作业,我有一个参考"类型的 ArrayList.Reference 是Book"类和Journal"类的父类.如果允许我将Book"和Journal"类型的对象添加到 Arraylist,如果我想通过以下代码访问 Book 和 Journal 的方法,为什么会出现错误?

For an assignment, I have an ArrayList of type "Reference". Reference is a parent class to the "Book" class and "Journal" class. If I am allowed to add objects of type "Book" and "Journal" to the Arraylist, why would I be getting an error if I want to access methods of Book and Journal via the following code?

          ArrayList.get([someindex]).someBookorJournalMethod()

arraylist 本身是父类,我想访问的方法只为书籍或期刊定义.

The arraylist itself is of the parent class, and the methods I want to access are only defined for either book or either journal.

这是一些代码

  public class Books extends Reference{

   private String Authors;
   private String Publishers;


      public Books(String CallNum, String Author, String Title, String Publisher, int year,String type)
      {

          super(CallNum,Title,year,type);

          Authors= Author;
          Publishers=Publisher;
      }

public String getAuthor()
{
    return Authors;
}



 public class LibrarySearch {


      private ArrayList<Reference> Library;


      public LibrarySearch()
      {
          Library = new ArrayList<Reference>(100);
      }


      public outputLibrary(){

      for (int i = 0 ; i < Library.size(); i+++)
      {
        if (Library.get(i).getType().equals("Book"))
        {
            System.out.println("Type:book\n" + "Call Number:" +  Library.get(i).getCallNumber() +"\n" + "Authors:" + Library.get(i).getAuthors();)
        }
    }

}

IntelliJ 对 Library.get(i).getAuthors() 行有问题,因为它是特定于 Books 的方法.我将如何解决这个问题?

IntelliJ is having issues with the line Library.get(i).getAuthors() because it is a method specific to Books. How would I resolve this?

推荐答案

因为当您指定变量的类型时,您只能调用为此类型定义的方法.例如,如果您有

Because when you specify the type of a variable, you can only invoke methods that are defined for this type. For example, if you have

public class A {
  public void methodA() {
    System.out.println("A");
  }
}

public class B extends A {
  public void methodB() {
    System.out.println("B");
  }
}

public class Main {
  public static void main(String[] args) {
    A ab = new B();
    ab.methodB();
  }
}

这不会编译,因为为变量 ab 定义的类型是 A 并且唯一可见的方法是那些在 A 中定义的方法>.

This will not compile, since the type defined for the variable ab is A and the only visible methods are those that are defined in A.

在您的情况下,您可以简单地在 Reference 中添加一个空白方法(如果您不想使类抽象):

In your case you can simply add a blank method in Reference(if you don't want to make the class abstract):

public void someBookorJournalMethod() {}

或者您可以显式地转换您试图为其调用方法的对象.

Or you can explicitly cast the object you're trying to invoke the method for.

但是,重要的是要注意这两种方法通常都是不好的做法,应该避免.如果实例化 Reference 对象没有意义,那么将类设为 abstract 并将 someBookorJournalMethod 定义为抽象方法.在您的代码中,您很可能以错误的方式使用继承.

However, it is important to note that both approaches are usually bad practices and should be avoided. If it does not make sense to instantiate Reference objects, than make the class abstract and define someBookorJournalMethod as an abstract method. In your code you're most probably using inheritance in a wrong way.

这篇关于从超类的数组列表访问子类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆