代码需要能够同时引用两个库号,如果它们相等,则从数组中删除借用器。
它不允许我从另一个类运行方法,因为它是静态上下文。我不知道还能如何解决这个问题。
这是我到目前为止所拥有的:
public boolean removeBorrower(String libraryNumber)
{
if(libraryNumber == null)
return false;
else if(Borrower.getLibraryNumber().equals(libraryNumber)))
borrowers.remove(Borrower);
return true;
}
您需要
传递对要比较的其他Borrower
的引用:
public boolean removeBorrower(String libraryNumber, Borrower otherBorrower)
{
if(libraryNumber == null)
return false;
else if(otherBorrower.getLibraryNumber().equals(libraryNumber)))
borrowers.remove(otherBorrower);
return true;
}
以前,您尝试获取泛型Borrower
类的库号,这在概念上没有意义。使用此代码,您有一个特定的人来检查库编号。
您需要获取包含removeBorrower
方法的类的实例。
我不认为您不能从另一个类运行该方法(除非该方法包含在包私有类中并且客户端类不是同一包的一部分)。
也许你想说,如果不引用包含该方法的类的现有实例removeBorrwer
则不允许运行此方法。