0

大家好,我有一个程序是图书馆管理程序

我有一个名为 borrowHolding(holdingId)throws BalanceLow, InvalidHolding 的方法,我从模型接口调用该方法,该接口调用外观类,将方法调用传递给库类,但从这里方法定义在接口借用者中声明,另一个接口成员扩展了这个借用者接口然后 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 我不知道该方法不能被调用不知道为什么。

4

1 回答 1

0

member属性永远不会在您的类中初始化,因此每次尝试使用它时Library都会得到一个。NullPointerException

由于member是接口的实例,Member因此您不能直接构造它。为此,您应该提供一个实现该Member接口的类。我发布了两个如何解决此问题的示例:

  1. 使用实现接口的类:

    public class MemberImpl implements Member {
        public void borrowHolding(int holdingId)
            throws InsufficientCreditException, MultipleBorrowingException {
            //some code here...
        }
    }
    
    public class Library {
        public Member member = new MemberImpl();
        //...
    }
    
  2. 实例化一个实现接口的匿名类:

    public class Library {
        public Member member = new Member() {
            public void borrowHolding(int holdingId)
                throws InsufficientCreditException, MultipleBorrowingException {
                //some code here...
            }
        };
        //...
    }
    

A̶l̶t̶e̶r̶n̶a̶t̶i̶v̶e̶l̶y̶,̶ ̶s̶i̶n̶c̶e̶ ̶y̶o̶u̶ ̶h̶a̶v̶e̶ ̶a̶d̶d̶e̶d̶ ̶a̶n̶ ̶i̶m̶p̶l̶e̶m̶e̶n̶t̶a̶t̶i̶o̶n̶ ̶t̶o̶ ̶t̶h̶e̶ ̶ ̶b̶o̶r̶r̶o̶w̶H̶o̶l̶d̶i̶n̶g̶̶ ̶m̶e̶t̶h̶o̶d̶ ̶i̶n̶s̶i̶d̶e̶ ̶ ̶M̶e̶m̶b̶e̶r̶̶ ̶i̶n̶t̶e̶r̶f̶a̶c̶e̶,̶ ̶t̶h̶i̶s̶ ̶m̶e̶a̶n̶s̶ ̶t̶h̶a̶t̶ ̶y̶o̶u̶ ̶w̶a̶n̶t̶e̶d̶ ̶t̶o̶ ̶d̶e̶c̶l̶a̶r̶e̶ ̶a̶ ̶c̶l̶a̶s̶s̶ ̶i̶n̶s̶t̶e̶a̶d̶ ̶o̶f̶ ̶a̶n̶ ̶i̶n̶t̶e̶r̶f̶a̶c̶e̶

public class Member implements Borrower {
    //...
}

O̶t̶h̶e̶r̶w̶i̶s̶e̶,̶ ̶i̶f̶ ̶ ̶M̶e̶m̶b̶e̶r̶̶ ̶i̶s̶ ̶i̶n̶d̶e̶e̶d̶ ̶a̶n̶ ̶i̶n̶t̶e̶r̶f̶a̶c̶e̶,̶ ̶r̶e̶m̶e̶m̶b̶e̶r̶ ̶t̶h̶a̶t̶ ̶i̶n̶t̶e̶r̶f̶a̶c̶e̶s̶ ̶*̶*̶d̶o̶ ̶n̶o̶t̶ ̶i̶m̶p̶l̶e̶m̶e̶n̶t̶ ̶a̶n̶y̶ ̶m̶e̶t̶h̶o̶d̶*̶*̶,̶ ̶t̶h̶e̶y̶ ̶j̶u̶s̶t̶ ̶d̶e̶f̶i̶n̶e̶ ̶t̶h̶e̶ ̶c̶o̶n̶t̶r̶a̶c̶t̶ ̶t̶h̶a̶t̶ ̶t̶h̶e̶ ̶i̶m̶p̶l̶e̶m̶e̶n̶t̶e̶r̶ ̶c̶l̶a̶s̶s̶ ̶m̶u̶s̶t̶ ̶f̶o̶l̶l̶o̶w̶.̶

编辑:

与接口类似,抽象类不能直接实例化,您需要一个扩展抽象类的类。该解决方案将类似于提供的解决方案:

  1. 使用直接扩展抽象类的AbstractMember类:

    public ConcretMember extends AbstractMember {
    }
    
    public class Library {
        public Member member = new ConcretMember();
        //...
    }
    
  2. 实例化一个扩展抽象类的匿名AbstractMember类:

    public class Library {
        public Member member = new AbstractMember() {
            @Override
            public void borrowHolding(int holdingId)
                throws InsufficientCreditException, MultipleBorrowingException {
                //some code here...
            }
        };
        //...
    }
    
于 2013-04-17T04:44:54.077 回答