gpt4 book ai didi

java - getMethod 处 new Class[]{} 的用法

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:49 26 4
gpt4 key购买 nike

两者之间有什么区别吗:

Method getIDMethod = MyInterface.class.getMethod("getId");

Method getIDMethod = MyInterface.class.getMethod("getId", new Class<?>[]{});

My界面如下:

public interface MyInterface {    
AnotherInterface getId();
}

最佳答案

不,没有区别。在第一种情况下,将隐式生成一个空的 Class[]

The Java Language Specification states

Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation (§15.12.4.2).

以及关于invocation and evaluating arguments

If m is being invoked with k ≠ n actual argument expressions, or, if m is being invoked with k = n actual argument expressions and the type of the k'th argument expression is not assignment compatible with T[], then the argument list (e1, ..., en-1, en, ..., ek) is evaluated as if it were written as (e1, ..., en-1, new |T[]| { en, ..., ek }), where |T[]| denotes the erasure (§4.6) of T[].

从技术上讲,它相当于

new Class[]{} // instead of new Class<?>[]{}

关于java - getMethod 处 new Class<?>[]{} 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22896321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com