具有固定/可变arity(varargs)匹配的最具体方法 [英] Most specific method with matches of both fixed/variable arity (varargs)

查看:130
本文介绍了具有固定/可变arity(varargs)匹配的最具体方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java语言的第15.12.2.5节中规范,它讨论了如何在具有固定arity的方法和可变arity方法的两种情况下选择最具体的方法(即 varargs )。

In section 15.12.2.5 of the Java Language Specification, it talks about how to choose the most specific method in both cases of methods with fixed arity and methods of variable arity (i.e. varargs).

我在JLS中找不到的是关于两个方法之间的决定,其中一个是固定的arity,另一个是变量arity 然而。例如:

What I can't find in the JLS is anything about deciding between two methods where one is of fixed arity and one of variable arity however. For example:

public interface SomeApi {
    public String getSomething(String arg);       // method 1
    public String getSomething(String ... args);  // method 2
}

按照预期编译就好( for下面Yoni概述的原因)。此调用代码也编译:

Compiles just fine as one would expect (for reasons outlined by Yoni below). This calling code compiles also:

SomeApi api = ...
Object o = api.getSomething("Hello");

如果你运行它,方法#1 (即非varargs方法)被调用。为什么这个调用代码不含糊?为什么固定arity方法比变量arnt方法更具体?有人能指出我的规范的相关位吗?

and if you run it, method #1 (i.e. the non-varargs method) is called. Why is this calling-code not ambiguous? Why is the fixed arity method more specific than the variable-arity one? Can someone point me to the relevant bit of the spec?

推荐答案

第一种方法解决阶段只考虑固定的arity方法和过程在考虑任何varargs方法之前,如果找到匹配则终止。

The first method resolution phase considers only fixed arity methods and the process is terminated if a match is found, before any varargs methods are considered.

来自 http://docs.oracle.com/javase/specs/jls/se6/html/expressions.html#15.12.2.2


15.12.2.2阶段1:识别匹配Arity 适用于
Subtyping的方法

15.12.2.2 Phase 1: Identify Matching Arity Methods Applicable by Subtyping

如果找不到通过子类型化
的方法,则搜索适用的
方法将继续第2阶段
(第15.12.2.3节)。 否则,在通过子类型应用
的方法中,选择
特定方法(第15.12.2.5节)

If no method applicable by subtyping is found, the search for applicable methods continues with phase 2 (§15.12.2.3). Otherwise, the most specific method (§15.12.2.5) is chosen among the methods that are applicable by subtyping.

(我的重点。)

这篇关于具有固定/可变arity(varargs)匹配的最具体方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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