我遇到错误 - 找不到符号 listOfBooks

I'm getting an error - cannot find symbol listOfBooks

我已经看了这个很久了,但我不明白为什么它找不到 listOfBooks。我在 public class Library 中定义错误了吗? 这是代码:

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

Book.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");
            }
        }
    }
}

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

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

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

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

这里是关于这个模式的更多信息:https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

In the spirit of encapsulation, it is common to make fields private. This means that they can only be directly accessed from the Bicycle class. We still need access to these values, however. This can be done indirectly by adding public methods that obtain the field values for us: