Java 中的函数重载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2047793/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:07:31  来源:igfitidea点击:

Function override-overload in Java

javaoverloadingoverriding

提问by Gandalf StormCrow

What is the difference between override and overload?

超载和过载有什么区别?

回答by Jon Skeet

  • Overloading: picking a method signatureat compile time based on the number and type of the arguments specified

  • Overriding: picking a method implementationat execution time based on the actual type of the target object (as opposed to the compile-time type of the expression)

  • 重载:在编译时根据指定参数的数量和类型选择方法签名

  • 覆盖:在执行时根据目标对象的实际类型(而不是表达式的编译时类型)选择方法实现

For example:

例如:

class Base
{
    void foo(int x)
    {
        System.out.println("Base.foo(int)");
    }

    void foo(double d)
    {
        System.out.println("Base.foo(double)");
    }
}

class Child extends Base
{
    @Override void foo (int x)
    {
        System.out.println("Child.foo(int)");
    }
}

...

Base b = new Child();
b.foo(10); // Prints Child.foo(int)
b.foo(5.0); // Prints Base.foo(double)

Both calls are examples of overloading. There are two methods called foo, and the compiler determines which signature to call.

这两个调用都是重载的例子。有两个方法被调用foo,编译器决定调用哪个签名。

The first call is an example of overriding. The compiler picks the signature "foo(int)" but then at execution time, the type of the target object determines that the implementation to use should be the one in Child.

第一个调用是覆盖. 编译器选择签名“foo(int)”,但是在执行时,目标对象的类型决定了要使用的实现应该是Child.

回答by miku

  • Overloading of methods is a compiler trick to allow you to use the same name to perform different actions depending on parameters.

  • Overriding a method means that its entire functionality is being replaced. Overriding is something done in a child class to a method defined in a parent class.

  • 方法的重载是一种编译器技巧,它允许您根据参数使用相同的名称来执行不同的操作。

  • 覆盖一个方法意味着它的整个功能都被替换了。覆盖是在子类中对父类中定义的方法进行的操作。

src: http://www.jchq.net/tutorial/06_02Tut.htm

源代码:http: //www.jchq.net/tutorial/06_02Tut.htm

回答by fastcodejava

Overloading :

重载:

public Bar foo(int some);
public Bar foo(int some, boolean x);  // Same method name, different signature.

Overriding :

覆盖:

public Bar foo(int some);   // Defined in some class A
public Bar foo(int some);   // Same method name and signature. Defined in subclass of A.

If the second method was not defined it would have inherited the first method. Now it will be replaced by the second method in the subclass of A.

如果未定义第二种方法,它将继承第一种方法。现在它将被 A 的子类中的第二个方法替换。

回答by Gaim

Overload - similar signature - same name, different parameters

重载 - 相似的签名 - 相同的名称,不同的参数

void foo() {
   /** overload */
}

void foo( int a ) {
   /** overload */
}

int foo() {
   /** this is NOT overloading, signature is for compiler SAME like void foo() */
}

Override - you can redefine method body when you inherit it.

覆盖 - 您可以在继承时重新定义方法体。

class A {
   void foo() {
      /** definition A */
   }
}

class B extends A {
   void foo() {
      /** definition B, this definition will be used when you have instance of B */
   }
}

回答by helpermethod

On interesting thing to mention:

关于有趣的事情:

public static doSomething(Collection<?> c) {
    // do something
}

public static doSomething(ArrayList<?> l) {
    // do something
}

public static void main(String[] args) {
    Collection<String> c = new ArrayList<String> ();
    doSomething(c); // which method get's called?
}

One would suppose the method with the ArrayList argument would be called but it doesn't. The first method is called since the proper method is selected at compile time.

人们会假设带有 ArrayList 参数的方法会被调用,但事实并非如此。第一个方法被调用,因为在编译时选择了正确的方法。

回答by vahidg

Override

覆盖

Is when a method which is inherited by a subclass from a superclass is replaced(overridden) in the subclass.

当子类从超类继承的方法在子类中被替换(覆盖)时。

class A {
  void foo() {
    /** definition A of foo */
  }
}

class B extends A {
  void foo() {
    /** definition B of foo */
  }
}

Now if you call foousing:

现在,如果您foo使用以下方法调用:

A a = new B();
a.foo();

The Bdefinition of foowill be run. This is not so intuitive since you will get a compile error if the class Adidn't have a method called foo. So the typeof the object awhich is Ahas to have the method foo, then you can call it, and the method fooof the instancewill be executed, which is that of class B, hence 'execution time'.

将运行的B定义foo。这不是那么直观,因为如果类A没有名为foo. 所以类型的对象aA具有有方法foo,那么你可以把它和方法foo的的情况下将被执行,这是阶级的B,因此是“执行时间”。

Overload

超载

When you create a method with the same name as an existing method. To avoid a compile time error, you haveto define the new method with different parameters than the existing one. This way the methods will be distinguishable. Have a method with the same name and parameters, but a different return type is still vague and will therefore cause a compile error. Example of overloading:

当您创建与现有方法同名的方法时。为避免编译时错误,您必须使用与现有方法不同的参数来定义新方法。这样,方法将是可区分的。具有相同名称和参数的方法,但不同的返回类型仍然是模糊的,因此会导致编译错误。重载示例:

class A {
  void bar(int i) {}
  // The following method is overloading the method bar
  void bar(Object a) {}
  // The following will cause a compile error. 
  // Parameters should differ for valid overload
  boolean bar(int i) {
    return true;
  }
}