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

问题描述

昨天我进行了两个小时的技术电话面试(我通过了,哇哦!),但我完全忽略了以下有关 Java 中动态绑定的问题.更令人费解的是,因为几年前我当助教的时候,我曾经向本科生教过这个概念,所以我给他们错误信息的前景有点令人不安......

Yesterday I had a two-hour technical phone interview (which I passed, woohoo!), but I completely muffed up the following question regarding dynamic binding in Java. And it's doubly puzzling because I used to teach this concept to undergraduates when I was a TA a few years ago, so the prospect that I gave them misinformation is a little disturbing...

这是给我的问题:

/* What is the output of the following program? */

public class Test {

  public boolean equals( Test other ) {
    System.out.println( "Inside of Test.equals" );
    return false;
  }

  public static void main( String [] args ) {
    Object t1 = new Test();
    Object t2 = new Test();
    Test t3 = new Test();
    Object o1 = new Object();

    int count = 0;
    System.out.println( count++ );// prints 0
    t1.equals( t2 ) ;
    System.out.println( count++ );// prints 1
    t1.equals( t3 );
    System.out.println( count++ );// prints 2
    t3.equals( o1 );
    System.out.println( count++ );// prints 3
    t3.equals(t3);
    System.out.println( count++ );// prints 4
    t3.equals(t2);
  }
}

我断言输出应该是来自重写的 equals() 方法中的两个单独的打印语句:在 t1.equals(t3)t3.equals(t3).后一种情况已经很明显了,对于前一种情况,即使t1有一个Object类型的引用,它被实例化为Test类型,所以动态绑定应该调用方法的重写形式.

I asserted that the output should have been two separate print statements from within the overridden equals() method: at t1.equals(t3) and t3.equals(t3). The latter case is obvious enough, and with the former case, even though t1 has a reference of type Object, it is instantiated as type Test, so dynamic binding should call the overridden form of the method.

显然不是.我的面试官鼓励我自己运行这个程序,瞧,被覆盖的方法只有一个输出:在 t3.equals(t3) 行.

Apparently not. My interviewer encouraged me to run the program myself, and lo and behold, there was only a single output from the overridden method: at the line t3.equals(t3).

我的问题是,为什么?正如我已经提到的,即使 t1 是一个 Object 类型的引用(所以静态绑定会调用 Object 的 equals() 方法),动态绑定应该 注意根据引用的实例化类型调用最具体的方法版本.我错过了什么?

My question then is, why? As I mentioned already, even though t1 is a reference of type Object (so static binding would invoke Object's equals() method), dynamic binding should take care of invoking the most specific version of the method based on the instantiated type of the reference. What am I missing?

推荐答案

Java 对重载方法使用静态绑定,对重写方法使用动态绑定.在您的示例中,equals 方法被重载(具有与 Object.equals() 不同的参数类型),因此调用的方法在编译时绑定到 reference 类型.

Java uses static binding for overloaded methods, and dynamic binding for overridden ones. In your example, the equals method is overloaded (has a different param type than Object.equals()), so the method called is bound to the reference type at compile time.

一些讨论这里

它是 equals 方法的事实并不真正相关,除了重载而不是覆盖它是一个常见的错误,根据你在面试中对问题的回答,你已经意识到了这一点.

The fact that it is the equals method is not really relevant, other than it is a common mistake to overload instead of override it, which you are already aware of based on your answer to the problem in the interview.

一个很好的描述这里.此示例显示了与参数类型相关的类似问题,但由相同的问题引起.

A good description here as well. This example is showing a similar problem related to the parameter type instead, but caused by the same issue.

我相信如果绑定实际上是动态的,那么调用者和参数是 Test 实例的任何情况都将导致调用被覆盖的方法.所以 t3.equals(o1) 将是唯一不会打印的情况.

I believe if the binding were actually dynamic, then any case where the caller and the parameter were an instance of Test would result in the overridden method being called. So t3.equals(o1) would be the only case that would not print.

这篇关于Java动态绑定和方法覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 04:35