Java的:覆盖或重载的方法?

编程入门 行业动态 更新时间:2024-10-27 15:30:25
本文介绍了Java的:覆盖或重载的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个方法,在一个名为PlaceParser即扩展类ModelParser

I have a method, in a class called "PlaceParser" that extends "ModelParser":

protected Place parseModel(JSONObject element) ...

一个地方是一个子类模型。如果 @覆盖标注添加到上述code?由于该方法具有不同的返回类型,这是否仍算作具有相同的名称和参数重写基类的方法/不返回类型改变签名?

A Place is a sub class of Model. Should the @Override annotation be added to the above code? As the method has a different return type, does this still count as overriding the base class method with the same name and arguments / does the return type alter the 'signature'?

在ModelParser的方法看起来像这样ModelT也延伸模式:

The "ModelParser" method looks like this "ModelT" also extends "Model":

protected abstract ModelT parseModel(JSONObject element) throws JSONException;

更新@乔恩飞碟双向:

的基类被声明如下:

public abstract class ModelParser<ModelT extends Model> {

我没有看到一个&LT; ModelT扩展型号方式&gt; 样式声明一个类之前

推荐答案

是的,你应该添加 @覆盖,因为你仍然覆盖方法。您正在使用的返回类型的协方差的事实并未改变。

Yes, you should add @Override, as you're still overriding the method. The fact that you're using covariance of return types doesn't change that.

特别是,如果其他code的类型的前pression ModelParser 键,通话 parseModel(元素),他们最终仍要多态中的您的实施。相比之下,与的重载的(例如通过增加另一个参数)凡在 ModelParser 原来的实现将被调用。

In particular, if other code has an expression of type ModelParser and calls parseModel(element) they will still end up polymorphically in your implementation. Compare that with overloading (e.g. by adding another parameter) where the original implementation in ModelParser would be called.

更多推荐

Java的:覆盖或重载的方法?

本文发布于:2023-06-11 23:28:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/645237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!