Java 重载与覆盖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/837864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java overloading vs overriding
提问by user69514
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
嗨,我只是想确保我有正确的这些概念。java 中的重载意味着您可以拥有一个构造函数或一个具有不同数量参数或不同数据类型的方法。IE
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;
}
Second question is: what is overriding in java? Does it relate to inheritance. Let's I have the following:
第二个问题是:java 中的重写是什么?是否与继承有关。让我有以下几点:
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;
}
}
So now let's say I have the following
所以现在假设我有以下内容
Truck truck = new Truck();
if I call
如果我打电话
truck.super.getPrice()
this would return the price from the Vehicle class, 20,000
这将返回 Vehicle 类的价格,20,000
if I call
如果我打电话
truck.getPrice()
this would return the price in the truck class, 14,000
这将返回卡车级别的价格,14,000
Is my knowledge correct for both questions?
我的知识对这两个问题都正确吗?
采纳答案by Eddie
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 signatureof 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;
}
This will fail to compile.
这将无法编译。
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:
正如 Rob 所确定的那样,我相信您的意思是覆盖,您的意思是正确的。注意覆盖,您不能更改返回类型。从 Java 5 开始,您可以返回基类方法返回的派生类型。在 Java 5 之前,它必须是相同的类型。也就是说,在 Java 5 及更高版本之前,您不能执行以下操作:
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 ();
}
}
However, even in Java 5 and later, you cannotdo the following:
但是,即使在 Java 5 及更高版本中,您也不能执行以下操作:
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.
最后,经常被忽视的重载和覆盖之间的一个很大区别是重载是在编译时决定的,而覆盖是在运行时决定的。当他们期望在运行时决定重载时,这让许多人感到惊讶。
回答by Rob
Correct; overloading is providing multiple signatures for the same method.
正确的; 重载是为同一方法提供多个签名。
Overriding, which is what I think you mean by "overwriting" is the act of providing a different implementation of a method inherited from a base type, and is basically the point of polymorphism by inheritance, i.e.
覆盖,这就是我认为您所说的“覆盖”是提供从基类型继承的方法的不同实现的行为,并且基本上是继承多态性的点,即
public class Bicycle implements Vehicle {
public void drive() { ... }
}
public class Motorcycle extends Bicycle {
public void drive() {
// Do motorcycle-specific driving here, overriding Bicycle.drive()
// (we can still call the base method if it's useful to us here)
}
}
回答by Luixv
what you have described is correct.
你所描述的是正确的。
For more clarification take a look at polymorphism concept. The Wikipedia has a good article
有关更多说明,请查看多态性概念。维基百科有一篇很好的文章
http://en.wikipedia.org/wiki/Polymorphism#Computing
http://en.wikipedia.org/wiki/Polymorphism#Computing
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming