实现接口的方法的返回类型
我很想知道处理实现接口的方法的返回类型的最佳实践/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 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).
嗯,你提到的协变返回,它在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.
您需要决定 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.
根据我的说法,返回类型应该是接口类型。原因是这样你可以稍后更改底层实现
我认为,如果我错了,请纠正我。
According to me,The return type should be that of Interface Type.Reason so that you can change the underlying implementation later
I think,Please correct me if im wrong.