2017-10-03 1 views
-6

Dies ist die erwartete Ausgabe:Klasse zurückzukehren

This Book 
title: The God and the Sword, 
year of publication: 1975, 
read: false 
This Book 
title: The God and the Sword, 
year of publication: 1975, 
read: true 
This Book 
title: The Light of Spirind, 
year of publication: 1960, 
read: false 
The book has now been read: true 

Dies ist der Code, den ich zur Zeit habe, brauche ich eine Klasse namens Buch zu schreiben. Könnte mir bitte jemand hier helfen? Ich bin verwirrt, wie das erste Buch einen Titel und ein Jahr hat und dann hat das zweite Buch kein Jahr zur Verfügung gestellt.

class Test{ 
    public static void main(String[] args){ 
     int year = 1975; 
     String title = "The God and the Sword"; 

     Book b1 = new Book(title, year); 
     System.out.println(b1); 
     b1.setRead(true); 
     System.out.println(b1); 

     title = "The Light of Spirind"; 
     Book b2 = new Book(title); 
     System.out.println(b2); 
     b2.setRead(true); 
     System.out.println("The book has now been read: " + b2.isRead()); 
    } 
} 

class Book{ 

    Book(String title, int year){ 
     System.out.println("title: " + title + ", " + "year of publication: " + year + ", " + "read: " + setRead); 

    } 
} 
+1

"Ich bin verwirrt, wie das erste Buch einen Titel und ein Jahr hat und dann die zweite hat kein Jahr zur Verfügung gestellt." Nun ja, Ihr aktueller Code wird nicht kompiliert, da Sie keinen 'Book (String)' -Konstruktor haben. –

+1

fügen Sie einen anderen Konstruktor hinzu: 'public Book (String title) {...}' –

+1

Da Ihre gewünschte Ausgabe ein Jahr für dieses Buch enthält, scheint es, dass Ihr 'main' falsch ist und es den Konstruktor verwenden sollte, der beides hat Titel und Jahr. – RealSkeptic

Antwort

0

Erste was mir auffiel, Buchbauer das Hauptproblem sind.

Ihr erster Konstruktor hat zwei Parameter title und year.

Book b1 = new Book(title, year);

Aber im zweiten Konstruktor hat einen Parameter title und Sie die year verpasst haben. Deshalb hast du year nicht bekommen.

Book b2 = new Book(title);

Sie können es richtig von der unter Anweisung die obige Aussage bearbeite.

Book b2 = new Book(title, year);

Zweite Sache versuchen Sie Objekt wie System.out.println(b2); ohne Implementierung toString() Methode zu drucken.

Bitte versuchen Sie folgenden Code. Sie können die erwartete Ausgabe erhalten, ohne den Konstruktor Book() direkt zu verwenden.

class Test { 

    public static void main(String[] args) { 

     Book b1 = new Book(); 
     b1.setYear(1975); 
     b1.setTitle("The God and the Sword"); 
     System.out.println(b1); 
     b1.setRead(true); 
     System.out.println(b1); 

     Book b2 = new Book(); 
     b2.setTitle("The Light of Spirind"); 
     b2.setYear(1960); 
     System.out.println(b2); 
     b2.setRead(true); 
     System.out.println("The book has now been read: " + b2.isRead()); 
    } 
} 

class Book { 

    private boolean read; 
    private int year; 
    private String title; 

    public boolean isRead() { 
     return read; 
    } 

    public void setRead(boolean read) { 
     this.read = read; 
    } 

    public int getYear() { 
     return year; 
    } 

    public void setYear(int year) { 
     this.year = year; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    Book() { 

    } 

    public String toString() { 
     return "This Book\ntitle: " + getTitle() + ",\n " + "year of publication: " + getYear() + ",\n " + "read: " + isRead(); 
    } 
} 
1

Ihre Klasse Book sollte zwei Konstrukteure hat, eine sowohl Titel und Jahr und eine einzigen Titel zu akzeptieren akzeptieren. Eine vollständigere Book Klasse ist die folgende:

class Book { 
    private String title; 
    private int year; 
    private boolean read; 

    public Book(String title) { 
     this.title = title; 
    } 

    public Book(String title, int year) { 
     this.title = title; 
     this.year = year; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public int getYear() { 
     return year; 
    } 

    public void setYear(int year) { 
     this.year = year; 
    } 

    public boolean isRead() { 
     return read; 
    } 

    public void setRead(boolean read) { 
     this.read = read; 
    } 
} 
0
Add single parameter constructor and check as follows 


class Test{ 
    public static void main(String[] args){ 
     int year = 1975; 
     String title = "The God and the Sword"; 

     Book b1 = new Book(title, year); 
     System.out.println(b1); 
     b1.setRead(true); 
     System.out.println(b1); 

     title = "The Light of Spirind"; 
     Book b2 = new Book(title); 
     System.out.println(b2); 
     b2.setRead(true); 
     System.out.println("The book has now been read: " + b2.isRead()); 
    } 
} 

class Book{ 

    Book(String title, int year){ 
     System.out.println("title: " + title + ", " + "year of publication: " + year + ", " + "read: " + setRead); 

    } 

Book(String title){ 
     System.out.println("title: " + title + "read: " + setRead); 

    } 
} 
Verwandte Themen