gpt4 book ai didi

java - 抽象类和继承者: is it possible to "pull up" .parent()在这里?

转载 作者:行者123 更新时间:2023-12-01 18:46:50 24 4
gpt4 key购买 nike

以下是我认为这两个类的代码的相关部分。第一,TreePointer (原始来源here):

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:

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 ,还是这只是一个白日梦?

最佳答案

当然,您可以添加 abstract方法parentTreePointer父类(super class)。真正的问题是,这有意义吗?我们实际上没有足够的上下文来知道它是否有意义。

如果它确实对 TreePointer 的所有情况都有意义,然后继续添加 abstract方法parentTreePointer并在所有子类中添加具体实现。或者,您可以将默认实现放在 TreePointer 中。并在您想要在子类中提供不同行为的地方重写它。

<小时/>

Also, how do I make sure that for an implementation X of TreeNode, the return type is X and not TreeNode? That is my problem, ultimately.

TreePointer类,你只需创建一个方法 public TreePointer<T> parent() 。您应该能够使所有代码通用,但 new new JsonPointer(...) 除外。部分。您可能必须将其重构为一个单独的方法(可能是 protected ),您可以在每个具体子类中重写该方法。

<小时/>

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

关于java - 抽象类和继承者: is it possible to "pull up" .parent()在这里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17382227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com