java – 基于参数的真实类型的重载方法选择


interface Callee {
    public void foo(Object o);
    public void foo(String s);
    public void foo(Integer i);

class CalleeImpl implements Callee
    public void foo(Object o) {
        logger.debug("foo(Object o)");

    public void foo(String s) {
        logger.debug("foo(\"" + s + "\")");

    public void foo(Integer i) {
        logger.debug("foo(" + i + ")");

Callee callee = new CalleeImpl();

Object i = new Integer(12);
Object s = "foobar";
Object o = new Object();;;;

这打印foo(Object o)三次。我期望方法选择考虑实际(不是声明的)参数类型。我缺少什么?有没有办法修改这个代码,以便它将打印foo(12),foo(“foobar”)和foo(Object o)?

I expect the method selection to take
in consideration the real (not the
declared) parameter type. Am I missing


引用Java Language Specification

When a method is invoked (§15.12), the
number of actual arguments (and any
explicit type arguments) and the
compile-time types of the arguments

are used, at compile time, to
determine the signature of the method
that will be invoked (§15.12.2). If
the method that is to be invoked is an
instance method, the actual method to
be invoked will be determined at run
time, using dynamic method lookup

转载请明显位置注明出处:java – 基于参数的真实类型的重载方法选择