-1

我已经看这个很久了,我不知道为什么它找不到listOfBooks。我是否在我的 中错误地定义了它public class Library?这是代码:

图书馆.java

import java.util.List;
import java.util.ArrayList;

public class Library {
    private List<Book> listOfBooks;
    private int capacity;
    private boolean libLimited;

    public Library() {
        libLimited = false;
    }

    public Library(int inCapacity) {
        libLimited = true;
        capacity = inCapacity;
    }


    public void addBook(Book newBook) {
        if (newBook != null && !newBook.equals("")) {
            throw new IllegalArgumentException("Can't be empty");
        }
        listOfBooks.add(newBook);
    }

    public String numberAvailableBooks() {
        boolean newBookPresent = false;
        for (Book newBook : listOfBooks) {
            if (!newBook.isBorrowed()) {
                return (newBook.getTitle());
                newBookPresent = true;
            }
        }
    }

    public int hasBookWithTitle(String bookTitle) {
        for (Book book : listOfBooks) {
            if (bookTitle.equalsIgnoreCase(listOfBooks.getTitle()) == true) {
                return true;
            }
        }
        return false;
    }

    public String getBookWithTitle(String bookTitle) {
        for (Book book : listOfBooks) {
            if (bookTitle.equlsIgnoreCase(listOfBooks.getTitle()) == true) {
                return listOfBooks;
            }
        }
        return null;
    }
}

图书.java

public class Book {
    private String title;
    private String author;
    private int copies;
    private boolean borrowed;

    public Book(String inAuthor, String inTitle, int inNumberOfCopies) {
        this.author = inAuthor;
        this.title = inAuthor;
        this.copies = inNumberOfCopies;
    }

    public void borrowed() {
        borrowed = true;
    }

    public void rented() {
        borrowed = true;
    }

    public void returned() {
        borrowed = false;
    }

    public boolean isBorrowed() {
        return borrowed;
    }


    public String getAuthor() {
        return this.author;
    }


    public String getTitle() {
        return this.title;

    }

    public int getTotalCopies() {
        return this.copies;
    }

    public int getAvailableCopies() {


    }

    public void withdrawCopy() {
        int found = 0;
        for (Book b : listOfBooks) {
            if (b.getTitle().equals(title)) {
                if (found == 0) {
                    found = 1;
                }
                if (!b.isBorrowed()) ;
                {
                    b.borrowed = true;
                    found = 2;
                    break;
                    throw new IllegalStateException("Nothing to Withdraw");
                }
            }
        }

    }

    public String returnCopy() {
        boolean found = false;
        for (Book book : listOfBooks) {
            if (book.getTitle().equals(title) && book.isBorrowed()) {
                book.returned();
                found = true;
                break;
                throw new IllegalStateException("Cannot Return");
            }
        }
    }
}

谁能告诉我我做错了什么?

4

1 回答 1

0

listOfBooks是 Library 类中的私有变量,因此无法在 Book 类中访问。您可能应该制作一个访问器方法,例如

public static List<Book> getListOfBooks() {
    return listOfBooks;
}

在您的图书馆类中,然后您可以通过调用来访问图书馆的其他类中的图书列表Library.getListOfBooks()。在这种情况下,您还需要将 listOfBooks 的声明更改为静态。您也可以将其设置为 listOfBooks 不是静态的,但是您必须将 Library 对象附加到 Book 对象才能访问此 listOfBooks 变量。

以下是有关此模式的更多信息:https ://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

本着封装的精神,将字段设为私有是很常见的。这意味着它们只能从 Bicycle 类直接访问。但是,我们仍然需要访问这些值。这可以通过添加为我们获取字段值的公共方法来间接完成:

于 2016-03-02T16:47:37.573 回答