The method setBook(int) is undefined for the type Lender上面的代码
时间: 2024-02-11 21:07:41 浏览: 163
谢谢指出!抱歉忘记了 Lender 类没有实现 `setBook(int)` 方法。可以在 `User` 类中添加一个 `protected` 访问修饰符的 `setBook(int)` 方法,这样 `Lender` 类就可以使用了。修改后的代码如下:
```
public class User implements IUser {
private String name;
private int book;
public User(String name, int book) {
this.name = name;
this.book = book;
}
@Override
public String getName() {
return name;
}
@Override
public int getBook() {
return book;
}
@Override
public void moreBook(int number) {}
protected void setBook(int book) {
this.book = book;
}
}
public class Lender extends User {
public Lender(String name, int booksLent) {
super(name, -booksLent); // lending books is the same as borrowing negative number of books
}
@Override
public void moreBook(int number) {
int currentBooksLent = -getBook(); // get the current number of books lent (which is stored as negative in User class)
currentBooksLent += number; // increase the number of books lent by the given number
setBook(-currentBooksLent); // set the book instance variable of User class to negative of current number of books lent
}
}
```
阅读全文