我怎样才能从toString返回一个对象

时间:2015-04-15 17:28:27

标签: java

我有这个任务,我想在一个对话框中打印书名,isbn号码和所有5本书的费用。我在for循环中遇到麻烦。当我试图从我的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[])
    {
        double charge;


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

        String msg = " ";



        for (int i = 0; i < bookArray.length; i++)
        {

            charge = bookArray[i].calculateTotal(quantityArray[i]);

            msg += String.format("Title ", this.getTitle()); //stuff to print


        }


        JOptionPane.showMessageDialog(null, msg);

    }




}

2 个答案:

答案 0 :(得分:2)

你想这样做:

for (int i = 0; i < bookArray.length; i++)
    {

        charge = bookArray[i].calculateTotal(quantityArray[i]);

        msg += String.format("Title ", bookArray[i].getTitle()); 

    }

您正在获取某本书的标题(由bookArray [i]引用)

答案 1 :(得分:0)

我认为这可能是这个问题的一个问题:

  msg += String.format("Title ", this.getTitle()); //stuff to print

试试这个:

  for (int i = 0; i < bookArray.length; i++)
            {

                charge = bookArray[i].calculateTotal(quantityArray[i]);

                msg += String.format("Title %s", bookArray[i].getTitle()); //stuff to print


            }