大家好我有一个图书馆管理程序
我有一个名为borrowHolding(holdingId)的方法抛出BalanceLow,InvalidHolding 我从模型接口调用那个调用facade类,它将方法调用传递给库类但是从这里方法定义在接口borrower中声明,另一个接口成员扩展这个borrower接口然后AbstractMember实现borrowHoling方法可能小编码片段清除:
所有导入都是正确的,每个类和接口都以不同的密度分隔。
public interface LMSModel {
public void borrowHolding(int holdingId)
throws InsufficientCreditException, MultipleBorrowingException;
}
public class LMSFacade implements LMSModel {
private Library lib = new Library();
private Member member;
public void borrowHolding(int holdingId)
throws InsufficientCreditException, MultipleBorrowingException {
// TODO Auto-generated method stub
lib.borrowHolding(holdingId);
}
}
public class Library {
public Member member;
public void borrowHolding(int holdingId) throws InsufficientCreditException, MultipleBorrowingException {
member.borrowHolding(holdingId);
}
}
public interface Member extends Borrower{
public abstract Holding[] getCurrentHoldings();
public abstract double getMaxCredit();
public abstract void resetCredit();
public abstract String getFullName();
public abstract String getMemberId();
}
public abstract class AbstractMember implements Member, Borrower {
public void borrowHolding(int holdingId)
throws InsufficientCreditException, MultipleBorrowingException {
// TODO Auto-generated method stub
System.out.println("Hello");
// Map<Integer, Holding> LibHoldingMap = libCollection.holdingMap;
// holding = (Holding)LibHoldingMap.get((Integer)holdingId);
//
// if (holding.isOnLoan()) {
// System.out.println("Can not be issued Currently on Load");
// } else {
// System.out.println("Can be issued");
// }
}
}
public interface Borrower {
void borrowHolding(int holdingId)throws InsufficientCreditException, MultipleBorrowingException;
}
当我调用borrowHolding时我得到nullPointerException我不知道该方法无法调用不知道为什么。
答案 0 :(得分:0)
member
属性永远不会在您的Library
课程中初始化,因此每次尝试使用时都会获得NullPointerException
。
由于member
是接口Member
的实例,因此无法直接构造它。为此,您应该提供一个实现Member
接口的类。我发布了两个如何解决这个问题的例子:
使用实现接口的类:
public class MemberImpl implements Member {
public void borrowHolding(int holdingId)
throws InsufficientCreditException, MultipleBorrowingException {
//some code here...
}
}
public class Library {
public Member member = new MemberImpl();
//...
}
实例化实现接口的匿名类:
public class Library {
public Member member = new Member() {
public void borrowHolding(int holdingId)
throws InsufficientCreditException, MultipleBorrowingException {
//some code here...
}
};
//...
}
可替换地,̶因为你必须加入一个实现到̶̶b̶o̶r̶r̶o̶w̶H̶o̶l̶d̶i̶n̶g̶
̶方法内̶̶M̶e̶m̶b̶e̶r̶
̶接口,̶这意味着你想声明一个类而不是一个接口
public class Member implements Borrower {
//...
}
否则,̶如果̶̶M̶e̶m̶b̶e̶r̶
̶确实是一个接口,̶记住接口̶*̶*不实现任何方法*̶*̶,̶他们只是限定合同中实施者类必须遵循。 ̶
编辑:
与接口类似,抽象类无法直接实例化,您将需要一个扩展抽象类的类。解决方案类似于提供的解决方案:
使用直接扩展抽象AbstractMember
类的类:
public ConcretMember extends AbstractMember {
}
public class Library {
public Member member = new ConcretMember();
//...
}
实例化扩展抽象AbstractMember
类的匿名类:
public class Library {
public Member member = new AbstractMember() {
@Override
public void borrowHolding(int holdingId)
throws InsufficientCreditException, MultipleBorrowingException {
//some code here...
}
};
//...
}