使用对象类的参数vararg和对象类的数组重载方法 [英] Method overloading with argument vararg of object class and array of object class

查看:91
本文介绍了使用对象类的参数vararg和对象类的数组重载方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试在类Varargdemo中重载方法flexiPrint(),则它将生成编译时错误.编译器将以下签名视为相同:

If I attempt to overload the method flexiPrint() in a class Varargdemo then it generates a compile time error. The compiler treats the following signatures the same:

public static void flexiPrint(Object... data){}
public static void flexiPrint(Object[] data){}

有人可以向我解释为什么他们会受到同样的对待吗?我找不到答案.

Can someone explain to me why they are treated the same? I haven't been able to find the answer.

推荐答案

Object...只是一个数组,与定义Object[]

Object... is nothing but it is an array, that means same as defining Object[]

...(三个点)表示 varargs 在Java中.

... (three dots) represents varargs in java.

我们通常会在主要方法(例如main(String... args)

We usually see this signature in main method like main(String... args)

因此,一个类中不允许有多个具有相同签名的方法(编译时错误).这就是为什么您会看到编译时错误.

So, having more than one method with same signature is not allowed in a class (compile time error). That is why you are seeing compile time error.

这篇关于使用对象类的参数vararg和对象类的数组重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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