从构造函数中调用重写的方法。改用什么呢?

时间:2019-03-27 06:29:47

标签: java oop inheritance constructor

我有一个Java代码,应该在基类的构造函数中调用重写的方法。 我了解根据许多参考资料可能会导致问题,例如:

Calling an overridden method from a parent class ctor

还有

https://medium.com/@rufuszh90/effective-java-item-17-design-and-document-for-inheritance-or-else-prohibit-it-be6041719fbc

我有问题要改用哪种模式?


在此处添加示例代码是一个Task构造函数:

 public <T extends AbstractCommon & IParent> Task(T parent, String uuid, String version, Set<AbstractNode> exceptions) throws Exception {
    Event tef = getExecutionFinishedEvent();
    Event tep = getExecutionInProgressEvent();
    Event tefailed = getExecutionFailed();
    Event tefinishedonallnodes = getExecutionFinishedOnAllNodesEvent();
    Event tTimeout = getTimeoutEvent();
    Event tNotAllNodesAreAcessible = getNotAllNodesAreAccessible();
    Event tNotAllObjectsAreAvailable = getNotAllObjectsAreAvailable();

}

这是扩展类AbstractRemoteTask,还有其他一些类。

public abstract class AbstractRemoteTask extends Task implements InterNodeProcessor {

public AbstractRemoteTask() {
}

@JsonIgnore
@Override
protected Event getExecutionFinishedEvent() {
    return getEvent(Event.EventType.EXECUTION_FINISHED_LOCAL);
}

@JsonIgnore
@Override
protected Event getExecutionFinishedOnAllNodesEvent() {
    return getEvent(Event.EventType.EXECUTION_FINISHED_ON_ALL_NODES);
}

@Override
protected Event getExecutionInProgressEvent() {
    return getEvent(Event.EventType.EXECUTION_IN_PROGRESS);
}

@JsonIgnore
@Override
protected Event getExecutionFailed() {
    return getEvent(Event.EventType.EXECUTION_FAILED);
}


@JsonIgnore
@Override
protected Event getTimeoutEvent() {
    return new RemoteEvent(this, getNodeUuid(), Event.EventType.TIMEOUT);
}

@JsonIgnore
@Override
protected Event getNotAllNodesAreAccessible() {
    return new RemoteEvent(this, getNodeUuid(), Event.EventType.NOT_ALL_NODES_ARE_ACCESSIBLE);
}

@JsonIgnore
@Override
protected Event getNotAllObjectsAreAvailable() {
    return new RemoteEvent(this, getNodeUuid(), Event.EventType.NOT_ALL_OBJECTS_ARE_AVAILABLE);
}

所以问题是实现此目标的最佳模式是什么?不建议采用这种方式。

0 个答案:

没有答案