Java的:覆盖或重载的方法? [英] Java: Overriding or Overloading method?

查看:146
本文介绍了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的:覆盖或重载的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆