如何在Java中制作一个可以接受任意数量的任何类型的参数的方法? [英] How to make a method which accepts any number of arguments of any type in Java?
本文介绍了如何在Java中制作一个可以接受任意数量的任何类型的参数的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以看到有一种方法可以让Java中的方法接受指定类型的任意数量的参数: http: //www.java-tips.org/java-se-tips/java.lang/how-to-pass-unspecified-number-of-arguments-to-am.html
I can see there is a way to have a method in Java which accepts any number of arguments of a specified type: http://www.java-tips.org/java-se-tips/java.lang/how-to-pass-unspecified-number-of-arguments-to-a-m.html
但是有没有一种方法可以接受任何类型的任意数量的参数?
but is there a way to make a method which accepts any number of arguments of any type?
推荐答案
所有Java对象都扩展了Object类.因此,您可以使函数接受一个Object数组:
All Java Objects extend the Object class. So you can make your function accept an Object array:
public void func(Object[] args) {
}
或者,如果您希望不传递任何内容:
Or if you want to be able to pass nothing:
public void func(Object... args) {
}
这篇关于如何在Java中制作一个可以接受任意数量的任何类型的参数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文