Java:重写或重载方法?
我有一个方法,在一个名为“PlaceParser”的类中,它扩展了“ModelParser”:
protected Place parseModel(JSONObject element) ...
Place 是 Model 的子类。 上面的代码中是否需要添加@Override注解?由于该方法具有不同的返回类型,这是否仍然算作覆盖具有相同名称和参数的基类方法/返回类型是否会改变“签名”?
“ModelParser”方法看起来像这个“ModelT”也扩展了“Model”:
protected abstract ModelT parseModel(JSONObject element)
throws JSONException;
更新@Jon Skeet:
基类声明如下:
public abstract class ModelParser<ModelT extends Model> {
我没有看到
之前类的样式声明。
I have a method, in a class called "PlaceParser" that extends
"ModelParser":
protected Place parseModel(JSONObject element) ...
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'?
The "ModelParser" method looks like this "ModelT" also extends "Model":
protected abstract ModelT parseModel(JSONObject element)
throws JSONException;
Update @Jon Skeet:
The base class is declared like this:
public abstract class ModelParser<ModelT extends Model> {
I hadn't seen a <ModelT extends Model>
style declaration for a class before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您应该添加
@Override
,因为您仍在重写该方法。您使用返回类型的协方差这一事实并没有改变这一点。特别是,如果其他代码具有
ModelParser
类型的表达式并调用parseModel(element)
,它们仍然会在您的实现中以多态方式结束。与重载(例如通过添加另一个参数)进行比较,其中将调用ModelParser
中的原始实现。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.In particular, if other code has an expression of type
ModelParser
and callsparseModel(element)
they will still end up polymorphically in your implementation. Compare that with overloading (e.g. by adding another parameter) where the original implementation inModelParser
would be called.重载 Java 以及多种编程语言允许您为多个方法重用一个方法名称。在某些情况下,您可能希望在同一个类中编写多个方法,这些方法使用不同的参数执行基本相同的工作。
当您编写代码来调用一种方法时,会根据您提供的一个或多个参数的类型选择适当的方法。
重载方法适用两条规则:
重写 在类层次结构中,当子类中的方法与超类中的方法具有相同的名称和类型签名时,则称子类中的方法覆盖了超类中的方法。
派生类中的方法可以与基类具有相同的名称。如果您有一个名为 book 的基类以及名为 book1 和 book2 的派生类,并且如果您在所有三个类中使用相同的方法,则将执行最后一个派生类方法,尽管在所有前面的类中都有名称相似的方法。 Java中的概念称为重写(Overriding)。
OVERLOADING Java, along with several programming languages,allows you to reuse a method name for more then one method . In some circumstances, you might want to write several methods in the same class that do basically the same job with different arguments.
When you write code to call one methods, the appropriate one is chosen according to the type of argument or arguments that you supply.
Two rules apply to overloaded methods:
OVERRIDING In the class hierarchy, when a methods in a sub class has the same name and type signature as method in the superclass, then the method in the subclass is said to override the method in superclass.
Methods in the derived class can have same name as that of the base class. If you are having one base class called book and to derived class called book1 and book2 and if you use same methods in all the three classes, then the last derived class method is executed although there are methods with similar names in all the former classes.The concept in Java is called as Overriding.
阅读更多内容:https://javarevisited.blogspot.com/2011/12/method-overloading-vs-method-overriding.html#ixzz3TbRYYM5z
Read more: https://javarevisited.blogspot.com/2011/12/method-overloading-vs-method-overriding.html#ixzz3TbRYYM5z