我有一个方法,在一个名为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的:覆盖或重载的方法?
发布评论