Java重载与覆盖 [英] Java overloading vs overriding

查看:103
本文介绍了Java重载与覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想确保我正确理解了这些概念. Java中的重载意味着您​​可以拥有具有不同数量的参数或不同数据类型的构造函数或方法.即

Hi I just want to make sure I have these concepts right. Overloading in java means that you can have a constructor or a method with different number of arguments or different data types. i.e

public void setValue(){
   this.value = 0;
}
public void setValue(int v){
   this.value = v;
}

这种方法怎么样?由于返回的数据类型不同,它是否仍会被视为重载?

How about this method? Would it still be considered overloading since it's returning a different data type?

public int setValue(){
   return this.value;
}


第二个问题是:什么是最重要的 在java中?它与继承有关吗?让我们有以下内容:


Second question is: what is overriding in java? Does it relate to inheritance. Let's I have the following:

public class Vehicle{
  double basePrice = 20000;
  //constructor defined
  public double getPrice(){
     return basePrice;
   }
}

public class Truck extends Vehicle{
  double truckPrice = 14000;
  //constructor defined
  public double getPrice(){
     return truckPrice;
   }
}

现在让我们说以下内容

Truck truck = new Truck();

如果我打电话

truck.super.getPrice()

这将返回车辆"类别的价格20,000

this would return the price from the Vehicle class, 20,000

如果我打电话

truck.getPrice()

这将返回卡车类的价格14,000

this would return the price in the truck class, 14,000

我的知识对两个问题都正确吗?

推荐答案

您基本上是正确的.重载在单个类中具有多个方法,其中方法具有相同的名称.但是,返回值不被视为该方法的 signature 的一部分.因此,不能通过仅更改返回值来重载方法.您的示例中不能包含以下代码:

You are basically correct. Overloading is having multiple methods in a single class where the method has the same name. However, the return value is not seen as part of the signature of the method. Thus, you cannot overload a method by changing only the return value. You cannot have the following code, from your example:

public void setValue() {
   this.value = 0;
}

public int setValue() {
   return this.value;
}

这将无法编译.

罗布(Rob)指出,我相信您的意思是 Overrideing (覆盖),并且您说的没错.请注意,您不能更改返回类型.从Java 5开始,您可以返回基类方法返回的派生类型.在Java 5之前,它必须是相同的类型.也就是说,在Java 5及更高版本之前,您不能执行以下操作:

As Rob identified, I believe you mean overriding, and you have that correct. Note with overriding, you cannot change the return type. As of Java 5, you can return a derived type of what the base class method returned. Before Java 5, it must be the identical type. That is, you cannot do the below until Java 5 and later:

public class AnimalNoise {}
public class Miaw extends AnimalNoise {}

public class Animal {
    public AnimalNoise makeNoise() {
        return new AnimalNoise();
    }
}

public class Cat extends Animal {
    public Miaw makeNoise() {
        return new Miaw ();
    }
}

但是,即使在Java 5和更高版本中,您也不能执行以下操作:

However, even in Java 5 and later, you cannot do the following:

public class Animal {
    public String makeNoise() {
        return "silence";
    }
}

public class Cat extends Animal {
    public Miaw makeNoise() {
        return new Miaw ();
    }
}
public class Miaw {}

最后,通常被忽略的重载和覆盖之间的巨大差异是,重载是在编译时确定的,而重载是在运行时确定的.当他们期望在运行时决定重载时,这使许多人感到惊讶.

Finally, a big difference between overloading and overriding that is often overlooked is that overloading is decided at compile time and overriding is decided at runtime. This catches many people by surprise when they expect overloading to be decided at runtime.

这篇关于Java重载与覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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