利用继承来打印引用

Utilizing Inheritance to Print a Reference

我有关于如何从多个代码区域调用变量以打印 ArrayList 的问题,这里是一个示例:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;

public class ReferenceHolder extends java.lang.Object{
ArrayList<Reference> references = new ArrayList<Reference>();



public void printAllBibtexEntries() {
    Reference.getBibtexEntry(null, null, null, 0, 0, 0);
    // TODO Auto-generated method stub

}

public void addReference(Book book ) {
    String author = Reference.getAuthor();
    System.out.println(references.size());
    references.add(new Book(author, null, null, 0));
    // TODO Auto-generated method stub
}

public void addReference(Article article) {
    references.add(new Article(null, null, 0, null, 0, 0));
    // TODO Auto-generated method stub


}

}

问题是我想从 "Book" 和 "Article" class 调用变量,利用来自 "addReference" 方法的信息并通过printAllBibtexEntries 方法。关于如何从 BibtexEntries 变量而不是 null 打印的任何提示?

P.S:即使我确实在 references.add() 中插入了变量,它仍然会 return "null." 示例在 addReference(Book书)关于 "author."

为Books/Articles

创建接口或抽象class

例如:

interface IReference {
     public String getAuthor();
}

class Book implements IReference {
    ... implement the interface
}

class Article implements IReference {
    ... implement the interface
}

现在您可以将列表声明为:

ArrayList<IReference> references = new ArrayList<IReference>();

public void printAllBibtexEntries() {
    IReference ref = (IReference)Reference.getBibtexEntry(null, null, null, 0, 0, 0);
    System.out.println(ref.getAuthor());
    // TODO Auto-generated method stub
}