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

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

问题描述

对于一个作业,我有一个类型为"Reference"的ArrayList.引用是"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.

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

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