可能的范围问题,多个类

时间:2016-07-30 23:45:01

标签: java arrays scope

我正在制作电子书程序,将电子书添加到库中,然后显示各种信息。我仍在使用该程序的某些部分,例如创建有效的语句来验证ISBN,但我将其保存以供日后使用。现在我只是尝试创建一个Ebook对象并将其添加到我的电子书阵列中。但是,当我尝试在EbookLibraryTest中调用addEbook时,我发现"找不到符号"对于ebook1.addEbook(...)的行。我很困惑,因为其他两个类编译。我是否正确地调用了该方法?如果是这样,还有什么其他问题导致此错误?

public class Ebook
{
    private String author = "";
    private String title = "";
    private double price = 0;
    private int isbn = 0;


    public Ebook(String author, String title, double price, int isbn)
    {
        this.author = author;
        this.title = title;
        this.price = price;

        if (isbn > 0)
        this.isbn = isbn;

        else
            isbn = 0;
    }   
        public void setPrice(double price)
        {
            if (price < 0)
            {
                    System.out.println("Invalid price");
            }

            else
                this.price = price;
        }
        public double getPrice()
        {

            if (price < 0 )
            {
                System.out.println("Invalid price");
                price = 0.0;
                return price;
            }
            else
                this.price = price;
                return price;


        }
        public void setAuthor(String theAuthor)
        {
            this.author = theAuthor;
        }
        public String getAuthor()
        {
            return author;
        }
        public void setIsbn(int isbn)
        {
            if (isbn > 0)
            {
                this.isbn = isbn;
            }
            else 
                isbn = 0;
        }
        public int getIsbn()
        {
            if (isbn > 0)
            {
                this.isbn = isbn;
                return isbn;
            }
            else
            System.out.println("Invalid isbn");
                            isbn = 0;
            return isbn;
        }
        public void setTitle(String title)
        {
            this.title = title;
        }
        public String getTitle()
        {

            return title;
        }
        public String toString()
        {
            return String.format("The author is %s, the title is %s, the price is %f, the isbn is%d,",
            author,title,price,isbn);
        }
}
public class EbookLibrary
{
    private int count = 0;
    private double total_cost = 0.0;

    Ebook[] ebooks = new Ebook[25];

    public EbookLibrary()
    {

    }
    public int getCount()
    {
        return count;
    }
    public double getCost()
    {
        return total_cost;
    }
    public String toString()
    {
        return String.format("The count is %d, the total cost is %f,", count, total_cost);
    }
        public void addEbook(String theAuthor, String aTitle, double thePrice, int theIsbn)
        {

            Ebook anEbook = new Ebook("blah", "thing", 1.0, 1);
            for (int counter = 0; counter < ebooks.length; counter++)
            {

            ebooks[counter] = anEbook;
            count++;
            price += total_cost;
            }

        }
}
public class EbookLibraryTest
{
    public static void main(String[] args)
    {
        Ebook ebook1 = new Ebook("Tom Sawyer", "The Title", 77.0, 33);
        Ebook ebook2 = new Ebook("Thing Do", "What What", 45.0, 15);
        Ebook ebook3 = new Ebook("Stephen King","The Thing",1.1, 7);
        Ebook ebook4 = new Ebook("Robert","A Title", 1.0, 1);
        Ebook ebook5 = new Ebook("Tom","Bad Title", 33.1, 17);
        Ebook ebook6 = new Ebook("Bob", "lol", 25.0, 15);

        ebook1.addEbook("Tom Sawyer", "The Title", 77.0, 33);


    }
}

1 个答案:

答案 0 :(得分:3)

您在addEbook()中定义了方法EbookLibrary,而不在Ebook中。但是,您试图在Ebook对象上调用它。

只需像这样调用库对象上的方法,它应该可以工作:

EbookLibrary myLibrary = new EbookLibrary();
myLibrary.addEbook(ebookAuthor, ebookTitle, ebookPrice, ebookIsbn);

假设ebookAuthorebookTitleebookPriceebookIsbn已在此代码段之前声明并分配。

您还可以重载addEbook()方法,只需将现有的电子书添加到您的库中,这样就可以像这样使用它:

myLibrary.addEbook(ebook1);