Java:“this”关键字的问题

时间:2013-07-24 11:00:13

标签: java package this non-static

我正在尝试使用类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 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个错误。有什么帮助吗?

6 个答案:

答案 0 :(得分:2)

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

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

阅读JLS.3 15.8

  

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


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

void borrowBook(String Borrower, String Due) {....... }

答案 1 :(得分:2)

在这个方法中

static void borrowBook(String Borrower, String Due) {

您无法在this上下文

中使用static

如我所见,不需要制作该方法static

更喜欢阅读Understanding Instance and Class Members

答案 2 :(得分:1)

我建议您使用IDE编码,因为这个问题可以由您自己理解。因为错误在this块中使用static

答案 3 :(得分: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;
    }

    }
}

答案 4 :(得分:0)

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

答案 5 :(得分:0)

只需更改此行:

static void borrowBook(String Borrower, String Due) {

public void borrowBook(String Borrower, String Due) {