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

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

问题描述

示例代码为:

    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是真的

对象调用


字符串调用
/ p>

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

第一次调用 test(null)使用<$ c调用方法$ c> 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)当我们放置时,再说如果条件:

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

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

它始终使用 String 参数。

编译时编译器是否会计算表达式(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:如果第二个和第三个操作数具有相同的类型(可能是null类型),那么这就是条件表达式的类型。)。在 A 中,第二个术语来自特定的类。由于这是更具体的, null 可以替换类 Object 的对象,整个表达式的类型是 Object (JLS:如果第二个和第三个操作数之一是null类型而另一个的类型是引用类型,那么条件表达式的类型是引用类型。)。

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.

您给出的如果的示例不同:您使用两个不同类型的对象调用方法。三元条件运算符始终在编译时中被评估为一个类型,适合这两个术语。

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天全站免登陆