从抽象类调用方法

时间:2012-11-02 12:29:25

标签: java spring hibernate inheritance

我有以下抽象类

public abstract class ReturnAgentFromTab extends BasePage{

    @Persist("session")
    public abstract Agent getAgent();
    public abstract void setAgent(Agent agent);

    @InjectObject("spring:agentApplicationModeResolver")
    public abstract AgentApplicationModeResolver getAgentApplicationModeResolver();

    .... more @InjectObject()

    public void nextPage(IRequestCycle cycle) {

        setApplicationModeUsingAgentStatus(getAgent());

        AgentSearchNavigationManager navManager = getAgentSearchNavigationManagerFactory().getAgentSearchNavigationManager();
        FlowStage stage = getFlowStage();
        if (stage == null) {
            setApplicationModeUsingAgentStatus(getAgent());
            stage = getUserDefaultFlowStageService().getDefaultFlowStage(UserHolder.getUser(), getVisitClass().getApplicationMode());
        }

        Class nextPageClass = navManager.getNextPage(getUserDefaultFlowStageService());

        String nextPageQualifier = getUserDefaultFlowStageService().getPageQualifier(getAgent(), nextPageClass, getVisitClass().getApplicationMode());
        IPage nextPage = getPageUtils().getPage(nextPageClass, nextPageQualifier);
        if ((getFlowStage() instanceof PSDFlowStage)) {
            nextPageQualifier = getFlowStage().getValue();
        }
        nextPage = getPageUtils().getPage(nextPageClass, nextPageQualifier);
        if (navManager instanceof NameBasedAgentSearchNavigationManager && nextPageClass != SignOffStatusPage.class) {
            NameBasedAgentSearchNavigationManager nameBasedNavManager = (NameBasedAgentSearchNavigationManager) navManager;
            String nextPageName = nameBasedNavManager.getNextPageName(stage); 
            if (!nextPageName.equals(nextPageClass.getSimpleName())) {
                nextPage = getPageUtils().getPage(nextPageName, nextPageQualifier);
            }
        }

        if (isNextPageActivateAgentGeneral(nextPage)) {
            initialisePageLink(nextPageClass, nextPage);
        }
        ((WuamsBasePage) nextPage).init(getAgent().getAgentId());
        getPageUtils().navigateTo(nextPage);

    }

    private void setApplicationModeUsingAgentStatus(Agent agent) {
        getVisitClass().setApplicationMode(getHomeLinksFactory().getRegionHomeLinksService().getApplicationMode(agent));
    }

    private boolean isNextPageActivateAgentGeneral(IPage nextPage) {
        return nextPage instanceof ActiveAgentGeneralPage;
    }

    private void initialisePageLink(Class nextPageClass, IPage nextPage) {
        if (getVisitClass().getPageLink() == null) {
            getVisitClass().setPageLink(PageLinkUtil.getPageLinkMessageKeyFromPageClass(nextPageClass, 
                    getUserDefaultFlowStageService().getDefaultFlowStage(UserHolder.getUser(), getVisitClass().getApplicationMode()).getValue()));
        }
    }

}

我想要做的是从另一个抽象的类中调用nextPage(cycle)并扩展ReturnAgentFromTab,但是当我尝试

public abstract class DoSomethingWithAgent extends ReturnAgentFromTab {

@Persist("session")
public abstract ReturnAgentFromTab getReturnAgentFromTab();
public abstract void setReturnAgentFromTab(ReturnAgentFromTab returnAgentFromTab);
....
getReturnAgentFromTab().nextPage(cycle);

我得到一个空指针异常,我知道这是因为我实际上并没有在任何地方设置ReturnAgentFromTab,但我不明白如何使用抽象类设置它。有人可以帮忙吗?

如果您需要更多代码,请询问

3 个答案:

答案 0 :(得分:2)

抽象类的目的是简单地不实现某些事物,例如提供某些对象。方法getReturnAgentFromTab()是一个很好的例子:类本身并不关心该对象的来源,因为这是子类的唯一责任。所以扩展那个类,编写那个方法,突然间基类做了它的事情。

答案 1 :(得分:1)

好吧,你不能初始化抽象类,唯一的方法是让一些其他具体类扩展你的抽象类,并用concrate类实例调用非抽象方法。

abstarct class ABS1 {
   //abstract methods
   //concreate method
   public void concMethod() {
      }
 }

public class ABS1Impl extends ABS1 {
 //implement all the abstract methods
 }
public abstract class ABS2 {
   ABS1 abs = new ABSImpl();
   abs.concMethod // 
}

答案 2 :(得分:1)

我认为这可能只是因为两个原因。

  1. 可能会getReturnAgentFromTab()返回null。或
  2. cycle的值为null