public class Book {
//variables
private String title = "";
int isbn = 0;
int quantity = 0;
public Book(String title, int isbn, int quantity) throws Exception {
this.setTitle(title);
this.setIsbn(isbn);
this.setQuantity(quantity);
}
//toString method that return with book information
public String toString() {
String s = "";
s = "title:" + this.title;
s = s + "\nIsbn:" + this.isbn;
s = s + "\nQuantity:" + this.quantity;
return s;
}
public void setTitle(String newTitle) throws Exception {
if ((newTitle != null)) {
this.title = newTitle;
} else {
BookException be = new BookException();
be.setMessage("wrong input, title should not be blank.");
throw be;
}
}
public String getTitle() {
return this.title;
}
}
why there was no symbol in the code this.setIsbn(isbn)
and this.setQuantity(quantity)
为什么代码中没有符号this.setIsbn(isbn)和this.setQuantity(quantity)
1 个解决方案
#1
1
Because your class do not have methods setIsbn and setQuantity you have to create them and then only you can use them
因为您的类没有方法setIsbn和setQuantity,所以您必须创建它们,然后才能使用它们
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
And on similar lines you should also have getters for these
在类似的路线上你也应该有吸气剂
public int getIsbn() {
return this.isbn;
}
public int getQuantity() {
return this.quantity;
}
#1
1
Because your class do not have methods setIsbn and setQuantity you have to create them and then only you can use them
因为您的类没有方法setIsbn和setQuantity,所以您必须创建它们,然后才能使用它们
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
And on similar lines you should also have getters for these
在类似的路线上你也应该有吸气剂
public int getIsbn() {
return this.isbn;
}
public int getQuantity() {
return this.quantity;
}