2016-04-18 197 views
-4

这是如果我的第一个问题在stackoverflow。我通常可以自己找到答案,但我遇到了这个问题。我有两个对象,“书”和“期刊”。这些是“发布”类的子类。现在,我正在尝试将3个“Book”实例和3个“Periodical”实例添加到ArrayList。我无法弄清楚如何做到这一点。 。将对象添加到ArrayList

有了这个当前的代码,我得到一个错误“找到加(图书,图书,图书,期刊,期刊,期刊)没有合适的方法

下面是当前的代码:

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, book2, book3, periodical1, periodical2, periodical3); 

要这样:

publications.add(book1); 
publications.add(book2); 
publications.add(book3); 
publications.add(periodical1); 
publications.add(periodical2); 
publications.add(periodical3); 

它摆脱了我的程序编译器错误,但它只是打印“periodical3”对象,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 

让我知道,如果你需要别的东西!

编辑x2:对不起,我意识到我的帖子越来越长。

因此,我已经从我的类变量或“数据字段”中删除了我在代码中调用它们的所有“静态”关键字。然后,我将我的代码更改回此代码:

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

它的工作原理!它执行它应该!我只有一个问题,虽然,这似乎代码,因为没有工作:

publications.add(book1, book2, book3, periodical1, periodical2,  periodical3); 

有一个较短的方式来添加所有的对象与出的ArrayList做逐一?

+4

从'Publication'类(及其子类)内的所有字段中移除'static'。 –

+0

这是相当多的猜测:)。可能是真的。你可以发布你的“出版”和“书”类吗? – Tunaki

+3

最有可能重复[为什么我的ArrayList包含添加到列表中的最后一个项目的N个副本?](http://stackoverflow.com/q/19843506/1393766) – Pshemo

回答

-1

如果我正确地理解了这个问题,那么就有6 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); 

然后我会看到它打印5,而不是4,描述你所看到的,因为你已经分配的所有变量的情况下在发布类别中,与您在最后一次拨打new Periodical期间使用的类别相同。

如果您想让一个类的多个实例具有其自己的值,则解决方案是不要使用static变量。

+0

我明白你在说什么,这很有道理!所以,我已经摆脱了出版物,期刊和书籍中所有变量的“静态”键盘。虽然我仍然收到相同的编译器错误。 –

+0

我回来了,那确实修好了!非常感谢你!我再次编辑了我的问题。 –