2016-04-18 19 views
-4

Dies ist meine erste Frage zu Stackoverflow. Ich kann normalerweise selbst Antworten finden, aber ich habe Probleme mit diesem. Ich habe 2 Objekte, "Book" und "Periodical". Dies sind Unterklassen einer Klasse "Publikation". Jetzt versuche ich 3 Instanzen von "Book" und 3 Instanzen von "Periodical" zu einer ArrayList hinzuzufügen. Ich habe Probleme herauszufinden, wie das geht. .Hinzufügen von Objekten zu ArrayList

Mit diesem aktuellen Code, ich einen Fehler „kein geeignetes Verfahren für add (Buch, Buch, Buch, Zeitschrift, Zeitschrift, Zeitschrift) gefunden erhalten, ist

Hier ist der aktuelle Code:

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 

ich habe auch versucht zu ändern:

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

Um dies:

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

Das löscht mein Programm des Compiler-Fehlers, aber dann druckt es nur das "periodic3" -Objekt, 6-mal. Ich bin mir nicht sicher, was ich falsch mache. Irgendwelche Vorschläge? Vielen Dank im Voraus! :)

EDIT:

Hier ist meine Klasse Buch:

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 

Meine Periodical Klasse ist identisch, aber hier ist meine Publikationsklasse:

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 

mich, wenn Sie wissen lassen, Brauchen sie sonst noch etwas!

EDIT x2: Tut mir leid, ich merke, dass meine Post ziemlich lang wird.

Also habe ich alle "statischen" Schlüsselwörter aus meinen Klassenvariablen oder "Datenfelder", wie ich sie in meinem Code genannt habe, losgeworden. Ich habe dann meinen Code zurück zu diesem Code:

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

Und es funktioniert! Es führt aus wie es sollte! Ich nur eine Frage aber, da dieser Code scheint nicht zu funktionieren:

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

Gibt es einen kürzeren Weg alle Objekte auf die Arraylist mit, um hinzuzufügen eines nach dem anderen zu tun?

+4

Entfernen Sie 'static' aus allen Feldern innerhalb der' Publication'-Klasse (und ihrer Unterklassen). –

+0

Das ist ziemlich raten :). Könnte aber wahr sein. Könnten Sie Ihre "Publication" - und "Book" -Klasse veröffentlichen? – Tunaki

+3

Sehr wahrscheinlich Duplikat von [Warum enthält meine ArrayList N Kopien des letzten zu der Liste hinzugefügten Objekts?] (Http://Stackoverflow.com/q/19843506/1393766) – Pshemo

Antwort

-1

Wenn ich das Problem richtig verstehe, haben Sie 6 Publication Objekte, und Sie sehen nur die Werte der zuletzt erstellten.

, die wahrscheinlich verursacht werden würde, weil Sie staticKlasse Variablen statt Instanz Variablen.

Zum Beispiel

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; 
    } 
} 

Wenn ich diese

A a1 = new A(4); 
A a2 = new A(5); 
System.out.println(a1.x); 

Dann laufen würde ich es 5 sehen drucken und nicht mehr als 4, was das Szenario beschrieben, die Sie sehen, weil Sie alle Variablen zugewiesen haben in der Veröffentlichungsklasse zu denen, die Sie während des letzten Aufrufs von new Periodical verwenden.

Die Lösung besteht darin, static Variablen nicht zu verwenden, wenn Sie mehrere Instanzen einer Klasse mit ihren eigenen Werten haben möchten.

+0

Ich sehe was du sagst, das macht Sinn! Also habe ich die "statische" Tastatur von allen meinen Variablen innerhalb von Publikation, Zeitschrift und Buch losgeworden. Ich bekomme jedoch immer noch den gleichen Compilerfehler. –

+0

Ich nehme das zurück, das hat es behoben! Vielen Dank! Ich habe meine Frage noch einmal bearbeitet. –

Verwandte Themen