首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >抽象类和继承者:可以在这里“拉起”.parent()吗?

抽象类和继承者:可以在这里“拉起”.parent()吗?
EN

Stack Overflow用户
提问于 2013-06-30 00:17:06
回答 2查看 208关注 0票数 0

下面是我认为的这两个类代码的相关部分。首先,TreePointer (原始源here):

代码语言:javascript
复制
public abstract class TreePointer<T extends TreeNode>
    implements Iterable<TokenResolver<T>>
{
    //...

    /**
     * What this tree can see as a missing node (may be {@code null})
     */
    private final T missing;

    /**
     * The list of token resolvers
     */
    protected final List<TokenResolver<T>> tokenResolvers;

    /**
     * Main protected constructor
     *
     * <p>This constructor makes an immutable copy of the list it receives as
     * an argument.</p>
     *
     * @param missing the representation of a missing node (may be null)
     * @param tokenResolvers the list of reference token resolvers
     */
    protected TreePointer(final T missing,
        final List<TokenResolver<T>> tokenResolvers)
    {
        this.missing = missing;
        this.tokenResolvers = ImmutableList.copyOf(tokenResolvers);
    }

    /**
     * Alternate constructor
     *
     * <p>This is the same as calling {@link #TreePointer(TreeNode, List)} with
     * {@code null} as the missing node.</p>
     *
     * @param tokenResolvers the list of token resolvers
     */
    protected TreePointer(final List<TokenResolver<T>> tokenResolvers)
    {
        this(null, tokenResolvers);
    }

    //...

    /**
     * Tell whether this pointer is empty
     *
     * @return true if the reference token list is empty
     */
    public final boolean isEmpty()
    {
        return tokenResolvers.isEmpty();
    }

    // .iterator(), .equals(), .hashCode(), .toString() follow
}

然后是JsonPointer,它包含这个我想在这里分解的.parent()方法(原始源here

代码语言:javascript
复制
public final class JsonPointer
    extends TreePointer<JsonNode>
{
    /**
     * The empty JSON Pointer
     */
    private static final JsonPointer EMPTY
        = new JsonPointer(ImmutableList.<TokenResolver<JsonNode>>of());

    /**
     * Return an empty JSON Pointer
     *
     * @return an empty, statically allocated JSON Pointer
     */
    public static JsonPointer empty()
    {
        return EMPTY;
    }

    //...

    /**
     * Return the immediate parent of this JSON Pointer
     *
     * <p>The parent of the empty pointer is itself.</p>
     *
     * @return a new JSON Pointer representing the parent of the current one
     */
    public JsonPointer parent()
    {
        final int size = tokenResolvers.size();
        return size <= 1 ? EMPTY
            : new JsonPointer(tokenResolvers.subList(0, size - 1));
    }

    // ...
}

正如主题中提到的,我这里遇到的问题是JsonPointer.parent()方法。事实上,这个方法背后的逻辑适用于所有TreeNode实现。除了我必须使用构造函数,当然这样的构造函数是依赖于实现的:/

有没有办法实现.parent(),这样TreeNode的每个实现都会收到自己的一个实例,而不是TreeNode,或者这只是一个白日梦?

EN

回答 2

Stack Overflow用户

发布于 2013-06-30 00:38:43

当然,您可以将abstract方法parent添加到TreePointer超类中。真正的问题是,这有意义吗?我们真的没有足够的上下文来知道它是否有意义。

如果它对TreePointer的所有情况都有意义,那么继续向TreePointer添加一个abstract方法parent,并在所有子类中添加一个具体的实现。或者,您可以在TreePointer中放置一个默认实现,并在您希望在子类中提供不同行为的地方覆盖它。

另外,我如何确保对于TreeNode的实现X,返回类型是X而不是TreeNode?归根结底,这是我的问题。

TreePointer类中,您只需将方法设置为public TreePointer<T> parent()。您应该能够使所有的代码在那里泛型,除了new new JsonPointer(...)部件。您可能必须将其重构为一个单独的方法(可能是protected),您可以在每个具体的子类中覆盖该方法。

既然您希望在子类中返回JsonPointer而不是TreePointer<T>,并且还存在需要(非静态)工厂方法的问题,我认为您最初的怀疑是非常正确的。您可以在父类中使用抽象实现来实现这一点,但您需要覆盖它以在子类中提供更具体的返回类型,我认为这不值得麻烦。只需在TreePointer中声明一个public abstract TreePointer<T> parent(),并在JsonPointer中用public JsonPointer parent()覆盖它。

票数 3
EN

Stack Overflow用户

发布于 2013-06-30 00:25:59

您可以在JsonPointer上创建工厂方法,这样您就不必使用构造函数,并且可以独立于实现。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17382227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档