简体   繁体   English

为什么在Java中不允许Foo(Object [])重载Foo(Object…)?

[英]Why is it not allowed in Java to overload Foo(Object…) with Foo(Object[])?

I was wondering why it is not allowed in Java to overload Foo(Object[] args) with Foo(Object... args) , though they are used in a different way? 我想知道为什么不允许Java用Foo(Object... args)重载Foo(Object[] args) Foo(Object... args) ,尽管它们以不同的方式使用?

Foo(Object[] args){}

is used like: 使用方式如下:

Foo(new Object[]{new Object(), new Object()});

while the other form: 而另一种形式:

Foo(Object... args){}

is used like: 使用方式如下:

Foo(new Object(), new Object());

Is there any reason behind this? 这背后有什么原因吗?

This 15.12.2.5 Choosing the Most Specific Method talk about this, but its quite complex. 15.12.2.5选择最具体的方法对此进行了讨论,但它相当复杂。 eg Choosing between Foo(Number... ints) and Foo(Integer... ints) 例如,在Foo(Number ... ints)和Foo(Integer ... ints)之间进行选择

In the interests of backward compatibility, these are effectively the same thing. 为了向后兼容,这些实际上是同一件事。

public Foo(Object... args){} // syntactic sugar for Foo(Object[] args){}

// calls the varargs method.
Foo(new Object[]{new Object(), new Object()});

eg you can define main() as 例如,您可以将main()定义为

public static void main(String... args) {

A way to make them different is to take one argument before the varargs 一种使它们与众不同的方法是在varargs之前接受一个参数

public Foo(Object o, Object... os){} 

public Foo(Object[] os) {}

Foo(new Object(), new Object()); // calls the first.

Foo(new Object[]{new Object(), new Object()}); // calls the second.

They are not exactly the same. 它们并不完全相同。 The subtle difference is that while you can pass an array to a varargs, you can't treat an array parameter as a varargs. 细微的差别是,尽管您可以将数组传递给varargs,但不能将数组参数视为varargs。

public Foo(Object... os){} 

public Bar(Object[] os) {}

Foo(new Object[]{new Object(), new Object()}); // compiles fine.

Bar(new Object(), new Object()); // Fails to compile.

Additionally, a varags must be the last parameter. 此外,变量必须是最后一个参数。

public Foo(Object... os, int i){} // fails to compile.

public Bar(Object[] os, int i) {} // compiles ok.

The most direct answer to the question is that having both declarations would create an ambiguity in the method lookup logic. 这个问题的最直接答案是拥有两个声明会在方法查找逻辑中造成歧义。 If you declared both in the same class, there would be no way to figure out which method you wanted when calling thusly: 如果您在同一个类中声明了两者,则无法确定因此调用时要使用的方法:

Object[] a = new Object[10];
Foo(a); // array or vararg?

And Java requires that there always be a most specific method for every method invocation. Java要求每次方法调用都必须有最特定的方法。 For the why , see Peter's answer. 关于为什么 ,请参阅彼得的答案。

暂无
暂无

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

相关问题 在Java中,为什么调用foo()没有给定2个varags方法foo(int ... ints)和foo(Object ... objects)? - In Java, why is the call foo() not ambigious given 2 varags methods foo(int… ints) and foo(Object… objects)? 为什么 foo(Object... obj) 是在 Java1.5 中实现的? - Why was foo(Object... obj) implemented in Java1.5? 为什么是富 <String> 被视为Foo的子类型(扩展) <? extends Object> ? - Why is Foo<String> considered a subtype (extended) of Foo<? extends Object>? 为什么列出 <? extends Foo> 不接受Foo类的对象? - Why does the List<? extends Foo> not accept an object of the Foo class? 为什么foo(1,2,3)没有作为整数[]传递给varargs方法foo(Object ...) - Why is foo(1,2,3) not passed to varargs method foo(Object… ) as an Integer[] 如何从Java访问Scala包对象中包含的对象Foo? - How to access an object Foo contained in a scala package object from Java? Are object initializations in Java “Foo f = new Foo() ” essentially the same as using malloc for a pointer in C? - Are object initializations in Java “Foo f = new Foo() ” essentially the same as using malloc for a pointer in C? 在Java中,我想以“ foo”的名称调用对象上的方法,但是“ foo”是字符串。 我该如何解决? - In Java, I want to call a method on an object by the name of 'foo', but 'foo' is a string. How do I get around this? 如何获得 <?> Foo的价值 <?> 宾语? - How to get the <?> value for a Foo<?> object? Java 8 Casting可选 <Object> 到long,int,String或Foo - Java 8 Casting Optional<Object> to long, int, String or Foo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM