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);
}
}
"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. –
fügen Sie einen anderen Konstruktor hinzu: 'public Book (String title) {...}' –
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