Java动态绑定和方法重写 [英] Java dynamic binding and method overriding

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

问题描述

昨天我接受了两个小时的技术电话采访(我通过了,哇喔!),但我完全消除了关于Java中动态绑定的以下问题。而且令人费解的是,因为几年前我曾经是TA的时候曾经教过这个概念,所以我给他们错误信息的前景有点令人不安......

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()方法中的语句:at 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()不同的参数类型),因此被调用的方法在编译时绑定到引用类型。

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动态绑定和方法重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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