0

クラス Library{} と Book{} を含む Java パッケージ「mylib」を作成しようとしています。

クラス Library{} のコードは次のとおりです。

/*
 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 context では使用できません。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) {

コンテキストthis では使用できませんstatic

ご覧のとおり、そのメソッドを作成する必要はありません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

このキーワードは、非静的メソッドから非静的変数を参照するために使用されます。静的メソッドから非静的変数を参照しています。

于 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 に答える