Java 向ArrayList添加对象

Java 向ArrayList添加对象,java,oop,object,arraylist,foreach,Java,Oop,Object,Arraylist,Foreach,这是我关于stackoverflow的第一个问题。我通常能自己找到答案,但我在这方面有困难。我有两件东西,书和期刊。这些是类发布的子类。现在,我尝试向ArrayList添加3个Book实例和3个期刊实例。我不知道怎么做 使用当前代码,我得到一个错误,没有找到addBook、Book、Book、Journal、Journal、Journal的合适方法 以下是当前代码: import java.util.ArrayList; import java.util.Date; public class

这是我关于stackoverflow的第一个问题。我通常能自己找到答案,但我在这方面有困难。我有两件东西,书和期刊。这些是类发布的子类。现在,我尝试向ArrayList添加3个Book实例和3个期刊实例。我不知道怎么做

使用当前代码,我得到一个错误,没有找到addBook、Book、Book、Journal、Journal、Journal的合适方法

以下是当前代码:

import java.util.ArrayList;
import java.util.Date;

public class DriverProgram {
  public static void main(String[] args) {
    // Instantiate 3 instances of each object.
    Book book1 = new Book(1234, 1, "James", 100, "Hello", "Berkwood Inc.",     new java.util.Date(), "History");
    Book book2 = new Book(2345, 2, "Ralph", 200, "Goodbye", "Shackles Co.", new java.util.Date(), "English");
    Book book3 = new Book(3456, 3, "Julia", 300, "Hello Again", "Trustin Inc.", new java.util.Date(), "History");
    Periodical periodical1 = new Periodical("Daily", "Dylan", "History 101", "History Inc.", new java.util.Date(), "History");
    Periodical periodical2 = new Periodical("Weekly", "Jannette", "Mathematics 101", "Mathematics Inc.", new java.util.Date(), "Mathematics");
    Periodical periodical3 = new Periodical("Monthly", "Patricia", "Science 101", "Science Inc.", new java.util.Date(), "Science");

    // Create an array list of the Publication class type, and add the objects to it.
    ArrayList <Publication> publications = new ArrayList<Publication>();
    publications.add(book1, book2, book3, periodical1, periodical2,     periodical3);

    // Pass the array list to a method to loop through it and display the     toString methods.
    displayObjects(publications);
  } // End of main

  static void displayObjects (ArrayList<Publication> publications) {
    // Loop through array list and display the objects using the toString     methods.
  for (Publication p : publications) {
      System.out.print(p.toString());
    } // End of for each loop
  } // End of displayObjects
} // End of DriverProgram class
为此:

publications.add(book1);
publications.add(book2);
publications.add(book3);
publications.add(periodical1);
publications.add(periodical2);
publications.add(periodical3);
这消除了我的程序的编译器错误,但它只打印周期性的3对象,6次。我不确定我做错了什么。有什么建议吗?提前谢谢你!:

编辑:

这是我的图书课:

public class Book extends Publication{
  private static int isbn = 0;
  private static int libraryOfCongressNbr = 0;
  private static String author = "";
  private static int nbrOfPages = 0;

  // Constructor for Book class with parameters for each attribute.
  public Book(int newISBN, int newLibraryOfCongressNbr, String newAuthor,     int newNbrOfPages, String newTitle, String newPublisher, java.util.Date     newPublicationDate, String newSubject) {
    super(newTitle, newPublisher, newPublicationDate, newSubject);
    isbn = newISBN;
    libraryOfCongressNbr = newLibraryOfCongressNbr;
    author = newAuthor;
    nbrOfPages = newNbrOfPages;
  }

/////////////////////////////////////////////////////// Getters     ///////////////////////////////////////////////////////

  int getISBN() {
    return isbn;
  }

  int getLibraryOfCongressNbr() {
    return libraryOfCongressNbr;
  }

  String getAuthor() {
    return author;
  }

  int getNbrOfPages() {
    return nbrOfPages;
  }

/////////////////////////////////////////////////////// Setters     ///////////////////////////////////////////////////////

  void setISBN(int newISBN) {
    isbn = newISBN;
  }

  void setLibraryOfCongressNbr(int newLibraryOfCongressNbr) {
    libraryOfCongressNbr = newLibraryOfCongressNbr;
  }

  void setAuthor(String newAuthor) {
    author = newAuthor;
  }

  void setNbrOfPages(int newNbrOfPages) {
    nbrOfPages = newNbrOfPages;
  }

  //toString method for Book class
  public String toString () {
    StringBuilder result = new StringBuilder();
    result.append("\nISBN: " + isbn + "\n");
    result.append("\nPublisher: " + libraryOfCongressNbr + "\n");
    result.append("\nAuthor: " + author + "\n");
    result.append("\nNumber of Pages: " + nbrOfPages + "\n");
    result.append("---------------------------------------------------------    ");
    return super.toString() + result.toString();
  } // End of toString
} // End of Book class
我的期刊课是一样的,但这里是我的出版课:

import java.util.Date;

public abstract class Publication {

  // Data fields.
  private static String title = "";
  private static String publisher = "";
  private static java.util.Date publicationDate;
  private static String subject = "";

  // Constructor for Publication class with parameters for each attribute.
  public Publication(String newTitle, String newPublisher, java.util.Date     newPublicationDate, String newSubject){
    title = newTitle;
    publisher = newPublisher;
    publicationDate = newPublicationDate;
    subject = newSubject;
  }

/////////////////////////////////////////////////////// Getters     ///////////////////////////////////////////////////////

  String getTitle() {
    return title;
  }

  String getPublisher() {
    return publisher;
  }

  java.util.Date getPublicationDate() {
    return publicationDate;
  }

  String getSubject() {
    return subject;
  }

/////////////////////////////////////////////////////// Setters     ///////////////////////////////////////////////////////

  void setTitle(String newTitle) {
    title = newTitle;
  }

  void setPublisher(String newPublisher) {
    publisher = newPublisher;
  }

  void setPublicationDate(java.util.Date newPublicationDate) {
    publicationDate = newPublicationDate;
  }

  void setSubject(String newSubject) {
    subject = newSubject;
  }

  //toString method for Publication class
  public String toString () {
    StringBuilder result = new StringBuilder();
    result.append("\nTitle: " + title + "\n");
    result.append("\nPublisher: " + publisher + "\n");
    result.append("\nPublication Date: " + publicationDate + "\n");
    result.append("\nSubject: " + subject + "\n");
    return result.toString();
  } // End of toString
} // End of Publication class
如果你还需要什么,请告诉我

对不起,我意识到我的帖子越来越长了

因此,我已经从我的类变量或者我在代码中调用的数据字段中去掉了所有静态关键字。然后,我将代码更改回以下代码:

ArrayList <Publication> publications = new ArrayList<Publication>();
publications.add(book1);
publications.add(book2);
publications.add(book3);
publications.add(periodical1);
publications.add(periodical2);
publications.add(periodical3);

是否有一种较短的方法可以将所有对象逐个添加到ArrayList中

如果我理解正确,您有6个发布对象,并且您只看到最近创建的一个发布对象的值

这可能是因为您使用的是静态类变量而不是实例变量

比如说

class A {
    static int x; // class variable
    int y;        // instance variable

    public A(int val) {
        x = val; // All 'A' classes now have x = val;
        y = val; // Only 'this' class has y = val;
    }
}
如果让我来处理这件事

A a1 = new A(4);
A a2 = new A(5);
System.out.println(a1.x);
然后我会看到打印5,而不是打印4,它描述了您正在看到的场景,因为您已经将Publication类中的所有变量分配给了在上次调用new Journal时使用的变量


如果希望一个类的多个实例具有自己的值,则解决方案是不使用静态变量

从Publication类及其子类中的所有字段中删除static。这是一个很好的猜测:。也许是真的。你能发布你的出版物和图书类吗?很可能是我编辑的问题的副本,包括我的出版物和图书类。此外,我对任何错误表示歉意。我对整个静态的事情还是很不确定。我明白你在说什么,这是有道理的!所以,我已经从出版物、期刊和书籍中的所有变量中去掉了静态键盘。不过我还是遇到了同样的编译器错误。我把它收回去,这确实解决了它!非常感谢你!我再次编辑了我的问题。
class A {
    static int x; // class variable
    int y;        // instance variable

    public A(int val) {
        x = val; // All 'A' classes now have x = val;
        y = val; // Only 'this' class has y = val;
    }
}
A a1 = new A(4);
A a2 = new A(5);
System.out.println(a1.x);