简体   繁体   English

抽象类的调用方法

[英]Calling method from abstract class

I have the following abstract class 我有以下抽象类

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()));
        }
    }

}

What I want to do is call my nextPage(cycle) from another class that is abstract and extends ReturnAgentFromTab , but when I try 我想做的是从另一个抽象类中调用我的nextPage(cycle) ,该类是抽象类并扩展ReturnAgentFromTab ,但是当我尝试时

public abstract class DoSomethingWithAgent extends ReturnAgentFromTab {

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

I get a null pointer exception, I know this is because I am not actually setting ReturnAgentFromTab anywhere but I do not understand how to set it using abstract classes. 我得到一个空指针异常,我知道这是因为我实际上没有在任何地方设置ReturnAgentFromTab,但是我不明白如何使用抽象类进行设置。 Can anybody help? 有人可以帮忙吗?

If ye need more code just ask 如果您需要更多代码,只需询问

The point of abstract classes is to simply not implement certain things, such as providing certain objects. 抽象类的要点是根本不实现某些事情,例如提供某些对象。 The method getReturnAgentFromTab() is a perfect example: the class itself does not care where that object comes from because that is the sole responsibility of the subclass. 方法getReturnAgentFromTab()是一个完美的例子:类本身并不关心该对象的来源,因为那是子类的唯一责任。 So extend that class, write that method, and all of a sudden the base class does its thing. 因此扩展该类,编写该方法,然后基类突然执行其操作。

well, you cant intialize abstract class, the only way is to make some other concrete class extend your abstract class, and call the non abstract method with the concrate classes instance. 好吧,您不能初始化抽象类,唯一的方法是使其他一些具体类扩展您的抽象类,并使用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 // 
}

I think this can be only because of two reason. 我认为这可能仅出于两个原因。

  1. may be getReturnAgentFromTab() is returning null . 可能是getReturnAgentFromTab()返回null or 要么
  2. value of cycle is null . cycle值为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM