0

我正在尝试使用类 Library{} 和 Book{} 创建一个 Java 包“mylib”。

这是类库的代码{}:

/*
 Create collection of books
 List books and status
 User input:
   'B' - Borrow a book
   'R' - Reserve a book
   'I' - Return a book
   'X' - Exit program
*/

package mylib;

public class Library {

    public static void main(String[] args) {
        Book[] MyBooks = new Book[3];
        Book x;

        MyBooks[0] = new Book("The Lover's Dictionary", "Levithan, D.", 211);
        MyBooks[1] = new Book("White Tiger", "Adiga, A.", 304);
        MyBooks[2] = new Book("Thirteen R3asons Why", "Asher, J.", 336);

        for (int i = 0; i < MyBooks.length; i++) {
            x = MyBooks[i];
            System.out.println((i + 1) + " " + x.sTitle);
        }
    }
}

这是 Book{} 类的代码:

package mylib;

class Book {
    // Declare fields
    byte iStatus;
    int iPages;
    String sTitle, sAuthor;
    String sBorrowedBy, sReservedBy;
    String sDueDate, sReturnDate;

    public static final byte BORROWED = 0, AVAILABLE = 1, RESERVED = 2;

    // Constructor
    public Book(String Title, String Author, int Pages) {
        this.sTitle = Title;
        this.sAuthor = Author;
        this.iPages = Pages;
        this.iStatus = this.AVAILABLE;
    }

    // Borrow method
    static void borrowBook(String Borrower, String Due) {
        if (this.iStatus == this.AVAILABLE) {
            this.sBorrowedBy = Borrower;
            this.sDueDate = Due;
            this.iStatus = this.BORROWED;
        } else if (this.iStatus == this.RESERVED
                && this.sReservedBy == Borrower) {
            this.sBorrowedBy = Borrower;
            this.sDueDate = Due;
            this.sReservedBy = "";
            this.iStatus = this.BORROWED;
        }
    }

    /*
     * static int reserveBook(String Borrower) {
     * 
     * }
     * 
     * static void returnBook(String Return) {
     * 
     * }
     */
}

以上部分代码由教授给出。我注释掉了空方法并测试了程序只是为了看看它是否可以编译。

this 关键字有 14 个错误。有什么帮助吗?

4

6 回答 6

2

您不能this在静态上下文中使用,例如static方法。你为什么将你的borrowBook()方法设为static. 它应该是一个没有static关键字的实例方法。

static方法属于该类并由其所有实例共享。您可以使用类名直接调用它们,Book.borrowBook(....)如果发生这种情况,运行时将不知道this该上下文中指的是什么/哪个对象。

阅读JLS.3 15.8

关键字 this 只能用在实例方法、实例初始化器或构造器的主体中,或者在类的实例变量的初始化器中。如果它出现在其他任何地方,则会发生编译时错误。


在您的情况下,最好使该borrowBook()方法成为实例方法,因为它会更改调用对象的状态,即修改其属性。只需更改方法声明并删除static

void borrowBook(String Borrower, String Due) {....... }
于 2013-07-24T11:01:27.800 回答
2

在这个方法中

static void borrowBook(String Borrower, String Due) {

您不能thisstatic 上下文中使用

如我所见,没有必要制作那种方法static

喜欢阅读理解实例和类成员

于 2013-07-24T11:02:03.210 回答
1

我建议您使用 IDE 进行编码,因为您自己会理解这个问题。因为错误在这里使用this在一个static块中。

于 2013-07-24T11:05:28.317 回答
1

由于您在静态上下文中使用它,它会给您错误。试试下面的代码:

package myLib;

class Book {
// Declare fields
byte iStatus;
int iPages;
String sTitle, sAuthor;
String sBorrowedBy, sReservedBy;
String sDueDate, sReturnDate;

public static final byte BORROWED = 0, AVAILABLE = 1, RESERVED = 2;

// Constructor
public Book(String Title, String Author, int Pages) {
    this.sTitle = Title;
    this.sAuthor = Author;
    this.iPages = Pages;
    this.iStatus = Book.AVAILABLE;
}

// Borrow method
//Remove the static keyword
//Refer to the remaining static variables like AVAILABLE OR BORROWED using Book and not this keyword.
 void borrowBook(String Borrower, String Due) {
    if(this.iStatus == Book.AVAILABLE) {
        this.sBorrowedBy = Borrower;
        this.sDueDate = Due;
        this.iStatus = Book.BORROWED;
    }
    else if(this.iStatus == Book.RESERVED && this.sReservedBy == Borrower) {
        this.sBorrowedBy = Borrower;
        this.sDueDate = Due;
        this.sReservedBy = "";
        this.iStatus = Book.BORROWED;
    }

    }
}
于 2013-07-24T11:13:16.213 回答
0

this关键字用于从非静态方法中引用非静态变量。您指的是静态方法中的非静态变量。

于 2013-07-24T11:01:51.993 回答
0

只需更改此行:

static void borrowBook(String Borrower, String Due) {

public void borrowBook(String Borrower, String Due) {
于 2013-07-24T11:06:16.117 回答