2016-04-18 207 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

我回來了,那確實修好了!非常感謝你!我再次編輯了我的問題。 –