Java:与“问题”;这";关键词
我试图用类库{}和图书{}制作一个Java包“mylib” 以下是类库{}的代码:Java:与“问题”;这";关键词,java,package,this,non-static,Java,Package,This,Non Static,我试图用类库{}和图书{}制作一个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 { p
/*
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);
}
}
}
上面的部分代码由教授给出。我注释掉了空方法,并测试了程序,看看它是否可以编译
我有14个这个关键字的错误。有什么帮助吗?您不能在静态上下文中使用此,比如
静态
方法。为什么要将您的borrowBook()
方法设置为static
。它应该是一个没有static
关键字的实例方法
static
方法属于该类,并由其所有实例共享。您可以使用类名直接调用它们,如Book.brookbook(..)
,如果发生这种情况,运行时将不知道该在该上下文中引用了什么/哪个对象
阅读
关键字this只能在实例方法、实例初始值设定项或构造函数的主体中使用,或者在类的实例变量的初始值设定项中使用。如果它出现在其他地方,则会发生编译时错误
在您的情况下,最好将
brookbook()
方法作为实例方法,因为它会更改调用对象的状态,即修改其属性。只需更改方法声明并删除static
:
void borrowBook(String Borrower, String Due) {....... }
您不能在静态上下文中使用
this
,比如static
方法。为什么要将您的borrowBook()
方法设置为static
。它应该是一个没有static
关键字的实例方法
static
方法属于该类,并由其所有实例共享。您可以使用类名直接调用它们,如Book.brookbook(..)
,如果发生这种情况,运行时将不知道该在该上下文中引用了什么/哪个对象
阅读
关键字this只能在实例方法、实例初始值设定项或构造函数的主体中使用,或者在类的实例变量的初始值设定项中使用。如果它出现在其他地方,则会发生编译时错误
在您的情况下,最好将
brookbook()
方法作为实例方法,因为它会更改调用对象的状态,即修改其属性。只需更改方法声明并删除static
:
void borrowBook(String Borrower, String Due) {....... }
该关键字用于引用非静态方法中的非静态变量。您引用的是来自静态方法的非静态变量。此关键字用于引用来自非静态方法的非静态变量。您指的是静态方法中的非静态变量。在此方法中
static void borrowBook(String Borrower, String Due) {
您不能在
静态
上下文中使用此
如我所见,没有必要使该方法成为静态的
喜欢用这种方法阅读
static void borrowBook(String Borrower, String Due) {
您不能在
静态
上下文中使用此
如我所见,没有必要使该方法成为静态的
我建议您使用IDE进行编码,因为这个问题您自己可以理解。因为这里有错误,所以在
静态块中使用这个。我建议您使用IDE进行编码,因为这个问题您自己可以理解。因为这里有错误,所以在静态块中使用此。只需更改此行:
static void borrowBook(String Borrower, String Due) {
到
只需更改这一行:
static void borrowBook(String Borrower, String Due) {
到
因为您是在静态上下文中使用它,所以它会给您带来错误。请尝试以下代码:
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;
}
}
}
因为您是在静态上下文中使用它,所以它会给您带来错误。请尝试以下代码:
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;
}
}
}
请不要以大写字母开头变量名。如果变量来自教授,我会尽快更改。请不要以大写字母开头变量名。参考教授的变量,我会尽快改变它。+1表示建议IDE:)。错过了那一点。+1表示建议IDE:)。错过了那一点。谢谢,程序编译完成了。为什么我必须用Book来代替它?在你的程序中,可用的、保留的和借用的变量本质上是静态的。可以使用类的名称直接引用变量。它们与类关联,而不是与类的对象关联。此外,它们在本质上是最终的。这意味着它们不能被类的对象更改,并且只有变量的一个副本可供所有对象使用。因此,用“书”代替“这个”。请参考@Suresh Atta发布的链接以进一步了解。谢谢,程序已编译。为什么我必须用Book来代替它?在你的程序中,可用的、保留的和借用的变量本质上是静态的。可以使用类的名称直接引用变量。它们与类关联,而不是与类的对象关联。此外,它们在本质上是最终的。这意味着它们不能被类的对象更改,并且只有变量的一个副本可供所有对象使用。因此,用“书”代替“这个”。请参考@Suresh Atta发布的链接以进一步了解。谢谢,我会看那篇文章。谢谢,我会看那篇文章。我应该也把它放到公共类吗?我应该也把它放到公共类吗?