Java:重写或重载方法?

发布于 2024-11-17 21:06:51 字数 625 浏览 4 评论 0原文

我有一个方法,在一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林迷了鹿 2024-11-24 21:06:51

是的,您应该添加 @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 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.

花期渐远 2024-11-24 21:06:51

重载 Java 以及多种编程语言允许您为多个方法重用一个方法名称。在某些情况下,您可能希望在同一个类中编写多个方法,这些方法使用不同的参数执行基本相同的工作。
当您编写代码来调用一种方法时,会根据您提供的一个或多个参数的类型选择适当的方法。
重载方法适用两条规则:

  1. 方法的返回类型可以不同,但​​重载方法的参数列表必须不同。
  2. 调用语句的参数列表必须有足够的差异,以便能够明确确定要调用的正确方法。

重写 在类层次结构中,当子类中的方法与超类中的方法具有相同的名称和类型签名时,则称子类中的方法覆盖了超类中的方法。
派生类中的方法可以与基类具有相同的名称。如果您有一个名为 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:

  1. The return type of the methods can be different, but the argument lists of overloaded methods must differ.
  2. The arguments lists of the calling statement must differ enough to allow unambiguous determination of the proper method to call.

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.

胡渣熟男 2024-11-24 21:06:51

1) 方法重载和重写之间的第一个也是最重要的区别是,在 Java 中方法重载的情况下,方法的签名会发生变化,而在方法重写的情况下,方法的签名保持不变。

2)Java中方法重载与重写之间的第二个主要区别是,您可以在一个类中重载方法,但重写只能在子类上完成。

3) 你不能重写Java中的staticfinalprivate方法,但你可以重载Java中的static、final或private方法.

4)Java中的重载方法采用静态绑定,重写方法采用动态绑定。

5) Java 中的私有方法和最终方法也不能被重写。

在此处输入图像描述

阅读更多内容:https://javarevisited.blogspot.com/2011/12/method-overloading-vs-method-overriding.html#ixzz3TbRYYM5z

1) First and most important difference between method overloading and overriding is that, In case of method overloading in Java, signature of method changes while in case of method overriding it remain same.

2) Second major difference between method overloading vs overriding in Java is that You can overload method in one class but overriding can only be done on subclass.

3) You can not override static, final and private method in Java but you can overload static, final or private method in Java.

4) Overloaded method in Java is bonded by static binding and overridden methods are subject to dynamic binding.

5) Private and final method can also be not overridden in Java.

enter image description here

Read more: https://javarevisited.blogspot.com/2011/12/method-overloading-vs-method-overriding.html#ixzz3TbRYYM5z

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文