简体   繁体   English

类型提升和方法重载(多选)

[英]Type Promotion and Method Overloading (Multiple Options)

What are the factors that determine which method will be executed when there are multiple acceptable methods because of type promotion?当由于类型提升而有多个可接受的方法时,决定执行哪个方法的因素是什么?

Here's example code这是示例代码

public class Demo {
  public static void main(String[] args) {
    byte a = 100;
    long b = 10000;
    test(a, b);
  }

  public static void test(long a, double b) {
    System.out.println("Method 2");
  }

  public static void test(int a, float b) {
    System.out.println("Method 1");
  }
}

The output is: Method 1 as written but Method 2 if I comment out test(int a, float b)输出是: Method 1如所写,但Method 2如果我注释掉test(int a, float b)

Why is that?这是为什么? Does it try to do the least amount of type promotion?它是否尝试进行最少的类型提升? Does it try to promote argument 1, then argument 2?它是否试图推广论点 1,然后是论点 2? Is it based on some type of priority?它是否基于某种类型的优先级?

I've seen this question: How method-overloading and primitive types works?我见过这个问题: 方法重载和原始类型如何工作? , which includes the statement: ,其中包括以下语句:

  1. If more than one method was identified, pick the most specific one.如果确定了不止一种方法,请选择最具体的一种。

I'm asking for more specifics behind how the final method to be executed is selected amongst all the possible methods.我正在询问如何在所有可能的方法中选择要执行的最终方法的更多细节。 I am aware that type promotion happens, but if there are multiple options after type promotion, how does the compiler determine the final method?我知道会发生类型提升,但是如果类型提升后有多个选项,编译器如何确定最终方法? In other words, from the statement above, what is more specific ?换句话说,从上面的陈述中,更具体的是什么?

Although the question and answer that you linked to already covers this to some extent, one can have a look at the more specific case here (pun intended). 尽管您链接到的问题和答案已经在某种程度上涵盖了这一点,但是可以在这里查看更具体的情况(双关语意味深远)。 Particulary, the relevant "path to the decision" referring to the (somewhat complex) description of 15.12.2.5. 特别地,相关的“决策路径”指的是15.12.2.5的(有些复杂)描述 Choosing the Most Specific Method in the JLS. 在JLS中选择最具体的方法

The section first of all says: 本节首先说:

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error. 非正式的直觉是,如果第一种方法处理的任何调用都可以传递给另一个方法而没有编译时错误,则一个方法比另一种方法更具体。

This already is very helpful, because you can see that whatever you're passing to the test(int, float) method could also be passed to the test(long, double) method. 这已经是非常有帮助的,因为你可以看到,无论你传递到test(int, float)方法也可以被传递到test(long, double)方法。 So the first one is more specific. 所以第一个更具体。

But referring to the spec: 但是请参考规范:

One applicable method m1 is more specific than another applicable method m2 , for an invocation with argument expressions e1, ..., ek , if any of the following are true: 如果满足以下任一条件,则对于使用参数表达式e1, ..., ek的调用,一个适用的方法m1比另一适用的方法m2更具体:

  • m2 is not generic, and m1 and m2 are applicable by strict or loose invocation, and where m1 has formal parameter types S 1 , ..., S n and m2 has formal parameter types T 1 , ..., T n , the type S i is more specific than T i for argument ei for all i (1 ≤ i ≤ n, n = k). m2不是通用的,并且m1m2可通过严格或宽松调用来应用,并且其中m1具有形式参数类型S 1 ,...,S n,m2具有形式参数类型T 1 ,...,T n ,则对于所有i(1≤i≤n,n = k),自变量ei类型S i比T i更具体。

... ...

A type S is more specific than a type T for any expression if S <: T 如果S <:T,则对于任何表达式,类型S比类型T更具体。

The latter refers to the 4.10. 后者指的是4.10。 Subtyping section where the the supertype relation :> is specified as the reflexive and transitive closure of the direct supertype relation > 1 , and the latter includes, for primitive types 类型部分,其中将超 类型关系 :>指定为直接超类型关系 > 1的自反和传递闭包,对于原始类型,后者包括

  • double > 1 float double > 1 float
  • long > 1 int long > 1个 int

So the method test(int, float) is more specific than test(long, double) because int is a subtype of long and float is a subtype of double . 因此方法test(int, float)test(long, double)更具体test(long, double)因为intlong的子类型,而floatdouble的子类型。


(Note that the concept of "subtypes" here is also applied to primitive types, and not only to "inheritance between classes") (请注意,此处的“子类型”概念也适用于基本类型,而不仅适用于“类之间的继承”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM