我在论坛上进行了一些研究,发现这最适合我的问题,但解决方案不起作用:accessing a variable from another class
因此,我试图访问“ LibraryCard”类中的两个变量:
private int limit;
private int booksBorrowed;
[我发现,如果要在第二个类“ Student”中访问它们,则必须在我的“ LibraryCard”类中添加一个get方法:
public int getlimit()
{
return this.limit;
}
public int getbooksBorrowed()
{
return this.booksBorrowed;
}
访问这两个变量后,我需要在我的“学生”类的if语句中使用它们:我已经以这种方式实现的
public boolean finishedStudies()
{
if ( (this.booksBorrowed = 0) && (this.booksBorrowed >= this.limit)) {
return true;
}
else
return false;
}
[当我尝试编译它时,BlueJ说它找不到可变的booksBorrowed和limit
我一般对Java和编程技术还是陌生的,将不胜感激。
您已经在Student类中创建了LibraryCard类的实例,然后可以通过在该实例上调用getter来访问这两个变量:
LibraryCard card = new LibraryCard();
int limit = card.getlimit();
int booksBorrowed = card.getbooksBorrowed();