java 这是重载,不同类中具有相同名称和不同签名的方法吗?

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

Is this Overloading, methods with same name in different classes and different signature?

javaoopoverloading

提问by Genocide_Hoax

If I have the following code in Java:

如果我在 Java 中有以下代码:

class A {

    public int add(int a , int b) {
        return (a+b);
    }
}

class B extends A {
    public float add(float a , float b) {
        return (a+b);
}

In this particular case the sub-class isn't exactly overriding the base class's addfunction as they have different signatures and the concept of overloading occurs only if they are in the same scope. So, is the function add(float , float)in the sub-class Btreated as an entirely new function and the concept of overloading and overriding is not applicable to it? And does it use 'Static binding' or 'Dynamic Binding'?

在这种特殊情况下,子类并没有完全覆盖基类的add功能,因为它们具有不同的签名,并且仅当它们在同一范围内时才会出现重载的概念。那么,add(float , float)子类中的函数是否B被视为一个全新的函数,重载和覆盖的概念不适用于它?它使用“静态绑定”还是“动态绑定”?

回答by Breavyn

Method add in class b is an overloadof add in class a. Not an override. An override would just be a different implementation of the original add method.

类 b 中的 add 方法是类 a中 add的重载。不是覆盖。覆盖只是原始 add 方法的不同实现。

回答by Cacho Santa

In that case you are not overridingthe method, since the signatures are different.

在这种情况下,您不会覆盖该方法,因为签名不同。

But there is overloadingin class b, since you have two methods with the same name but different parameters (one if class a, and the other one in class b)

但是在 b 类中存在重载,因为您有两个名称相同但参数不同的方法(一个是 a 类,另一个是 b 类)

Hope it helps.

希望能帮助到你。

回答by Brian Agnew

In brief, yes. To override, you need to replicate the complete method signature, which includes the method name, parameters and return types. From the tutorial

简而言之,是的。要覆盖,您需要复制完整的方法签名,其中包括方法名称、参数和返回类型。从教程

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

与超类中的实例方法具有相同签名(名称,加上其参数的数量和类型)和返回类型的子类中的实例方法会覆盖超类的方法。

You might want to consider the @Override annotation, which will trigger a compiler error if you don't successfully overrride a method.

您可能需要考虑@Override 注释,如果您没有成功覆盖方法,它将触发编译器错误。

In this particular instance, it perhaps looks like you don't need overriding so much as some solution including generics. So you could instantiate a class a<Integer>and a similar class a<Float>

在这个特定的例子中,看起来你不需要像一些解决方案那样覆盖泛型。所以你可以实例化一个类a<Integer>和一个类似的类a<Float>

回答by Prateek Sharma

There can be a method that is not overridden but overloaded in the subclass. Here the subclass has two add() methods. The version which accepts int arguments(not overridden), and the overloaded method add() which accepts float arguments.

可以有一个方法没有被覆盖但在子类中被重载。这里的子类有两个 add() 方法。接受 int 参数(未覆盖)的版本,以及接受浮点参数的重载方法 add()。

回答by R.V

I think in this particular case neither overloading nor overriding occurs, because return type must be same in case overloading and overriding, so neither static binding nor dynamic binding happens in this case. method overloading is not possible in case of different return type, because compiler can't figure that which method he need to call.

我认为在这种特殊情况下,不会发生重载和覆盖,因为在重载和覆盖的情况下返回类型必须相同,因此在这种情况下既不会发生静态绑定也不会发生动态绑定。在不同返回类型的情况下,方法重载是不可能的,因为编译器无法确定他需要调用哪个方法。

回答by Digvijay Ambule

I know it's late answer but i think it's important question need to be answered for beginners.

我知道这是迟到的答案,但我认为这是初学者需要回答的重要问题。

One key point in overloading is it works in inheritance.

重载的一个关键点是它在继承中起作用。

Next is either it's Static bindingor Dynamic binding.

接下来是它的Static bindingDynamic binding

It is Static BindingSo, why?

它是静态绑定那么,为什么?

Static Binding

静态绑定

  1. Static binding in Java occurs during Compile time.
  2. private, finaland staticmethods and variables uses static binding and bonded by compiler.
  3. Static binding uses Type (class in Java) information for binding.
  1. Java 中的静态绑定发生在Compile time.
  2. privatefinal以及static方法和变量用途静态结合和由编译器接合。
  3. 静态绑定使用类型(Java 中的类)信息进行绑定。

Dynamic Binding

动态绑定

  1. Dynamic binding occurs during Runtime.

  2. Dynamic methods bonded during runtime based upon runtime object.

  3. Dynamic binding uses Object to resolve binding.

  1. 动态绑定发生在Runtime.

  2. 基于运行时对象在运行时绑定的动态方法。

  3. 动态绑定使用 Object 来解析绑定。

But the important part is here

但重要的部分在这里

Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime.

重载的方法使用静态绑定绑定,而重写的方法在运行时使用动态绑定绑定。

Java compiler determines correct version of the overloaded method to be executed at compile time based upon the type of argument used to call the method and parameters of the overloaded methods of both these classes receive the values of arguments used in call and executes the overloaded method.

Java 编译器根据用于调用方法的参数类型确定在编译时要执行的重载方法的正确版本,这两个类的重载方法的参数都接收调用中使用的参数值并执行重载方法。

B a=new B();
a.add(4, 5);
a.add(4.0f, 5.0f);

So if you will create reference of type B then it will search for proper argument type and for above code it will execute both methods.

因此,如果您将创建类型 B 的引用,那么它将搜索正确的参数类型,并且对于上述代码,它将执行这两种方法。

A a=new B();
a.add(4, 5);
a.add(4.0f, 5.0f);

but for above code it will give compile time error for float arguments.

但是对于上面的代码,它会为浮点参数提供编译时错误。

Hope it clears all doubts.

希望能解开所有疑惑。

回答by Rahul

The concept of Overloading comes in play if and only if the functions are in the same scope or class. cz if this is the case of method overloading then for same method signature or same argument type the compiler gets confuse and must give compile time error . but in the above program in class B if you pass the same argument in same order then according to overloading it must give error but it is not happening u can check it i already have. It is the case of inheritance where through object reference if you call any method then the compiler will check it in child class ,if its not there then it will look into parent class that the above program is all about. hope this is helpfull.

当且仅当函数在相同的作用域或类中时,重载的概念才起作用。cz 如果这是方法重载的情况,那么对于相同的方法签名或相同的参数类型,编译器会感到困惑并且必须给出编译时错误。但是在上面的 B 类程序中,如果你以相同的顺序传递相同的参数,那么根据重载它必须给出错误,但它没有发生,你可以检查它我已经有了。这是继承的情况,如果您调用任何方法,则通过对象引用,编译器将在子类中检查它,如果它不存在,则它将查看上述程序所涉及的父类。希望这有帮助。

回答by Buhari Abdullahi Suleiamn

The method add()in class A is also available to class B by inheritance, therefore the method is overloadedin class by changing the data type from int, intto float, float.

add()类 A 中的方法也可以通过继承用于类 B,因此该方法overloaded通过将数据类型从 更改int, intfloat, float