简体   繁体   English

将子类传递给具有抽象类参数的方法

[英]Passing a subclass to a method with an abstract class parameter

I've been trying to understand what is happening with this example, but haven't been able to so far.我一直试图了解这个例子发生了什么,但到目前为止还没有。 Can anyone help?任何人都可以帮忙吗?

Say I have these two Abstract classes:假设我有这两个抽象类:

public abstract class AbstractBook<T extends AbstractPage> implements Serializable{
    public AbstractBook() {
    }

    public abstract void addPage(T var1);
}

and

public abstract class AbstractPage implements Serializable {
    public AbstractPage() {
    }
}

And classes that are extended like these:以及像这样扩展的类:

public class Book extends AbstractBook {

    private ArrayList<Page> allPages;
    private String title;

    Book(String title){
       allPages = new ArrayList<Page>();
    }

    @Override
    public void addPage(AbstractPage page) {
        this.allPages.add();
    }
}

and

public class Page extends AbstractPage {

  private String content;

  public Page(String Content){
    this.content = Content;
  }

}

In the Book class there is a method called addPage .Book类中有一个名为addPage的方法。 I can pass a Page class object to this method, however;但是,我可以将Page类对象传递给此方法; I can't add that object to the ArrayList<Page> allPages .我无法将该对象添加到ArrayList<Page> allPages

Why exactly is that?为什么会这样? I mean I think it is because the compiler expects type AbstractPage instead of Page .我的意思是我认为这是因为编译器需要类型AbstractPage而不是Page But why then I am capable of calling the method like thisbook.addPage(pageObject) ?但是为什么我能够调用thisbook.addPage(pageObject)类的方法?

So In other words, it lets me pass a subclass object, but it doesn't let me do anything with that passed object.所以换句话说,它让我传递一个子类对象,但它不允许我对传递的对象做任何事情。

And how do you get around this so that I can add that pageObject to the ArrayList<Page> allPages structure?你如何解决这个问题,以便我可以将该 pageObject 添加到ArrayList<Page> allPages结构?

You can make it work by modifying your Book class:您可以通过修改Book类来使其工作:

public class Book extends AbstractBook<AbstractPage> {

  private ArrayList<AbstractPage> allPages;
  private String title;

  Book(String title){
    allPages = new ArrayList<AbstractPage>();
  }

  @Override
  public void addPage(AbstractPage page) {
    this.allPages.add(page);
  }
}

You should use the type parameter of AbstractBook when defining the class Book .在定义类Book时,您应该使用AbstractBook的类型参数。 The ArrayList has to bee of type AbstractPage if you want to insert elements of this type.如果要插入这种类型的元素,则ArrayList必须是AbstractPage类型。

You can't add AbstractPage to list of Page s, because Page is one level up in terms of inheritance from AbstractPage : Object <- AbstractPage <- Page .您不能将AbstractPage添加到Page的列表中,因为Page是从AbstractPage继承的一级: Object <- AbstractPage <- Page If you want to add Page s to your list, you have to extend AbstractBook<Page> :如果要将Page s 添加到列表中,则必须扩展AbstractBook<Page>

public class Book extends AbstractBook<Page> {

    private ArrayList<Page> allPages;
    private String title;

    Book(String title){
       allPages = new ArrayList<Page>();
    }

    @Override
    public void addPage(Page page) {
        this.allPages.add();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM