我试图在一个对话框中打印书名和isbn。
我对java很新,我不能让toString()
工作,我需要帮助。
public class Book
{
private String title;
private String author;
private String isbn;
private Double price;
private Publisher publisher;
public Book()
{
setTitle("");
setAuthor("");
setIsbn("");
setPrice(0.0);
setPublisher(new Publisher());
}
public Book(String t, String a, String i, double p, Publisher n)
{
setTitle(t);
setAuthor(a);
setIsbn(i);
setPrice(p);
setPublisher(n);
}
public void setTitle(String t)
{
title = t;
}
public String getTitle()
{
return title;
}
public void setAuthor(String a)
{
author = a;
}
public String getAuthor()
{
return author;
}
public void setIsbn(String i)
{
isbn = i;
}
public String getIsbn()
{
return isbn;
}
public void setPrice(double p)
{
price = p;
}
public double getPrice()
{
return price;
}
public void setPublisher(Publisher n)
{
publisher = n;
}
public Publisher getPublisher()
{
return publisher;
}
public double calculateTotal(int quantity)
{
return(price * quantity);
}
public String toString()
{
return( "Title" + title + "Author" + author + "Isbn" + isbn
+ "Price" + price + "Publisher" + publisher.toString());
}
}
import javax.swing. JOptionPane;
public class BookTest
{
public static void main(String args[])
{
String dataArray[][] = {{"Abraham Lincoln Vampire Hunter","Grahame-Smith","978-0446563079","13.99", "Haper", "NY"},
{"Frankenstein","Shelley","978-0486282114","7.99","Pearson", "TX"},
{"Dracula","Stoker","978-0486411095","5.99","Double Day", "CA"},
{"Curse of the Wolfman"," Hageman","B00381AKHG","10.59","Harper", "NY"},
{"The Mummy","Rice","978-0345369949","7.99","Nelson", "GA"}}; //book info
Book bookArray[] = new Book[dataArray.length];
int quantityArray[] = {12, 3, 7, 23, 5};
for (int i = 0; i < dataArray.length; i++)
{
bookArray[i] = new Book(dataArray[i][0], dataArray[i][1], dataArray[i][2],
Double.parseDouble(dataArray[i][3]), new Publisher(dataArray[i][4], dataArray[i][5]));
}
for (int i = 0; i < bookArray.length; i++)
{
bookArray[i].calculateTotal(quantityArray[i]);
}
JOptionPane.showMessageDialog(null, Book.toString());
}
}
答案 0 :(得分:0)
当您通过类的名称限定方法而不是引用该类的实例的变量时,您尝试在类Book上调用名为toString的静态方法。您的Book类中未定义此类方法。
相反,你应该调用的是书籍实例上的toString(),例如bookArray中的那些实例
例如,这里是在循环内调用的字符串:
for (int i = 0; i < bookArray.length; i++)
{
Book b = bookArray[i];
b.calculateTotal(quantityArray[i]);
//Using JOptionPane.showMessageDialog here only for an example, this probably isn't the OPs intent
JOptionPane.showMessageDialog(null, b.toString(););
}
有关覆盖toString的额外阅读,我建议您使用short chapter in Effective Java, 2nd Edition.
答案 1 :(得分:0)
做的时候
JOptionPane.showMessageDialog(null, Book.toString());
您尝试静态调用toString
,Book
未定义。{/ p>
您需要从Book
实例调用它。
答案 2 :(得分:0)
你的代码看起来很模糊。
toString()的问题在以下代码部分
中for (int i = 0; i < bookArray.length; i++)
{
bookArray[i].calculateTotal(quantityArray[i]);
}
JOptionPane.showMessageDialog(null, Book.toString());
当您调用Book.toString()时,实际上是从类Book调用该方法。要打印书籍的内容,您应该在for循环中插入该代码并使用书籍实例。
for (int i = 0; i < bookArray.length; i++)
{
bookArray[i].calculateTotal(quantityArray[i]);
JOptionPane.showMessageDialog(null, bookArray[i].toString());
}
PS:请使用IDE及其AutoFormat实用程序。
对于Eclipse,您可以使用 Control + Shift + F 和Intellij IDEA Control + Alt + L