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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4924198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:38:21  来源:igfitidea点击:

Return type of method that implements an interface

javamethodstypesreturn

提问by Koh Zi Chun

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:

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

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

And we have a class TreeNode that implements that:

我们有一个类 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;
    }
}

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

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

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.

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

回答by Aravind Yarram

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).

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

回答by Gursel Koca

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

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

I would prefer TreeNode as return type at TreeNode class.

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

回答by Nathan Hughes

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.

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

回答by Deepak

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

根据我的说法,返回类型应该是 Interface Type.Reason 的类型,以便您以后可以更改底层实现

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.

我想,如果我错了,请纠正我。