实现接口的方法的返回类型

发布于 2024-10-16 06:32:37 字数 822 浏览 3 评论 0原文

我很想知道处理实现接口的方法的返回类型的最佳实践/SE 约定是什么。具体来说,假设我们正在实现一个简单的树,具有这样的接口:

public interface ITreeNode {
    public ITreeNode getLeftChild();
    public ITreeNode getRightChild();
    public ITreeNode getParent();
}

并且我们有一个实现该接口的类 TreeNode:

public class TreeNode implements ITreeNode {
    private TreeNode LeftChild, RightChild, Parent;
    @Override
    public ITreeNode getLeftChild() {
        return this.LeftChild;
    }

    @Override
    public ITreeNode getRightChild() {
        return this.RightChild;
    }

    @Override
    public ITreeNode getParent() {
        return this.Parent;
    }
}

我的问题是.. 相应实现方法的返回类型应该是 ITreeNode 还是 TreeNode,以及为什么。

Eclipse 会自动使用 ITreeNode 的返回类型填充 TreeNode 的方法。然而,即使使用 @Override 标志,将其更改为 TreeNode 也不会导致任何错误或警告。

I am curious to know what is the best practice / SE convention of handling return type of methods that implements an interface. Specifically, assume we are implementing a simple tree, with interface as such:

public interface ITreeNode {
    public ITreeNode getLeftChild();
    public ITreeNode getRightChild();
    public ITreeNode getParent();
}

And we have a class TreeNode that implements that:

public class TreeNode implements ITreeNode {
    private TreeNode LeftChild, RightChild, Parent;
    @Override
    public ITreeNode getLeftChild() {
        return this.LeftChild;
    }

    @Override
    public ITreeNode getRightChild() {
        return this.RightChild;
    }

    @Override
    public ITreeNode getParent() {
        return this.Parent;
    }
}

My question is.. should the return type of the respective implemented methods be ITreeNode or TreeNode, and why.

Eclipse automatically populate the methods for TreeNode with return type of ITreeNode. However changing it to TreeNode doesn't cause any errors or warnings even with the @Override flag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福%小乖 2024-10-23 06:32:37

从 Java 5 开始,重写的方法可以返回超类中提到的返回类型的接口/类的任何子类。除非特别需要,否则我将坚持返回接口(这主要发生在重构旧代码时,其中客户端已经使用特定的实现类,而我们现在正在重构它以形成类层次结构)。

Since Java 5, the overridden methods can return any subclass of the interface/class of the return type mentioned in super class. I will stick to returning the interface unless specifically required (this happens mainly while refactoring the old code where the client was already using a specific implementation class while we are now refactoring it to form a class hierarchy).

缱倦旧时光 2024-10-23 06:32:37

嗯,你提到的协变返回,它在jdk 1.5之前不存在。 Java 语言中添加了协变返回概念以支持 Java 泛型。有关详细信息,http://java.sun.com/j2se/1.5 /pdf/generics-tutorial.pdf

我更喜欢 TreeNode 作为 TreeNode 类的返回类型。

Well, what you have mentioned has called as covariant return, it did not exists prior to jdk 1.5. Covariant return concepts has been added to Java language to support Java Generics. For more information , http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

I would prefer TreeNode as return type at TreeNode class.

笑,眼淚并存 2024-10-23 06:32:37

您需要决定 TreeNode 对象应该做什么。如果它的节点除了实现ITreeNode的TreeNode之外还可以包含其他东西,那么不仅返回类型需要是ITreeNode,而且实例成员也需要是ITreeNode。否则,如果它一直都是 TreeNodes,那么我不清楚为什么你要为界面烦恼。

You need to decide what your TreeNode object should do. If its nodes can contain other things besides TreeNodes that implement ITreeNode, then not only the return types need to be ITreeNode, but the instance members also need to be ITreeNode. Otherwise if it's TreeNodes all the way down then it's not clear to me why you're bothering with an interface.

段念尘 2024-10-23 06:32:37

根据我的说法,返回类型应该是接口类型。原因是这样你可以稍后更改底层实现

1 Example> ITreeNode node = new Tree1();  // First Underlying Implementation     
2 Example> ITreeNode node1 = new Tree2(); // Second Underlying Implementation    

我认为,如果我错了,请纠正我。

According to me,The return type should be that of Interface Type.Reason so that you can change the underlying implementation later

1 Example> ITreeNode node = new Tree1();  // First Underlying Implementation     
2 Example> ITreeNode node1 = new Tree2(); // Second Underlying Implementation    

I think,Please correct me if im wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文