方法重载和选择最具体的类型 [英] Method overloading and choosing the most specific type

查看:21
本文介绍了方法重载和选择最具体的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码是:

    public class OverloadingTest {

       public static void test(Object obj){
           System.out.println("Object called");
       }

       public static void test(String obj){
           System.out.println("String called");
       }

       public static void main(String[] args){
           test(null);
           System.out.println("10%2==0 is "+(10%2==0));
           test((10%2==0)?null:new Object());
           test((10%2==0)?null:null);
   }

输出是:

被调用的字符串
10%2==0 为真
对象被调用
字符串调用

String called
10%2==0 is true
Object called
String called

test(null) 的第一次调用调用带有 String 参数的方法,根据 Java 语言规范 可以理解.

The first call to test(null) invokes the method with String argument , which is understandable according to The Java Language Specification .

1) 谁能解释一下在前面的调用中调用 test() 的依据是什么?

1) Can anyone explain me on what basis test() is invoked in preceding calls ?

2) 当我们再次放置时,说一个 if 条件:

2) Again when we put , say a if condition :

    if(10%2==0){
        test(null);
    }
    else
    {
        test(new Object());
    }

它总是使用 String 参数调用方法.

It always invokes the method with String argument .

编译器会在编译时计算表达式 (10%2) 吗?我想知道表达式是在编译时还是运行时计算的.谢谢.

Will the compiler compute the expression (10%2) while compiling ? I want to know whether expressions are computed at compile time or run time . Thanks.

推荐答案

Java 使用早期绑定.在编译时选择最具体的方法.最具体的方法是通过参数数量和参数类型来选择的.在这种情况下,参数的数量不相关.这给我们留下了参数的类型.

Java uses early binding. The most specific method is chosen at compile time. The most specific method is chosen by number of parameters and type of parameters. Number of parameters is not relevant in this case. This leaves us with the type of parameters.

参数有什么类型?两个参数都是表达式,使用三元条件运算符.问题简化为:条件三元运算符返回什么类型?类型是在编译时计算的.

What type do the parameters have? Both parameters are expressions, using the ternary conditional operator. The question reduces to: What type does the conditional ternary operator return? The type is computed at compile time.

给出两个表达式:

(10%2==0)? null : new Object(); // A
(10%2==0)? null : null; // B

类型评估的规则在这里列出.在 B 中很容易,这两个术语完全相同:null 将被返回(可能是什么类型)(JLS:如果第二个和第三个操作数具有相同的类型(可能是空类型),那么这就是条件表达式.").在 A 中,第二项来自特定的类.由于这是更具体的并且 null 可以替换为 Object 类的对象,整个表达式的类型是 Object(JLS:如果第二个和第三个操作数之一是空类型,另一个的类型是引用类型,那么条件表达式的类型就是那个引用类型.").

The rules of type evaluation are listed here. In B it is easy, both terms are exactly the same: null will be returned (whatever type that may be) (JLS: "If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression."). In A the second term is from a specific class. As this is more specific and null can be substituted for an object of class Object the type of the whole expression is Object (JLS: "If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.").

在表达式的类型评估之后,方法选择符合预期.

After the type evaluation of the expressions the method selection is as expected.

您给出的带有 if 的示例是不同的:您使用两种 不同 类型的对象调用方法.三元条件运算符总是在编译时被评估为一个类型,同时满足这两个条件.

The example with if you give is different: You call the methods with objects of two different types. The ternary conditional operator always is evaluated to one type at compile time that fits both terms.

这篇关于方法重载和选择最具体的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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