简体   繁体   English

如何创建一个接受Java中任何类型的任意数量的参数的方法?

[英]How to make a method which accepts any number of arguments of any type in Java?

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-am.html 我可以看到在Java中有一种方法可以接受任意数量的指定类型的参数: http//www.java-tips.org/java-se-tips/java.lang/how-to-传递非指定数的论点对am.html

but is there a way to make a method which accepts any number of arguments of any type? 但有没有办法使一个方法接受任何类型的任何数量的参数?

All Java Objects extend the Object class. 所有Java对象都扩展了Object类。 So you can make your function accept an Object array: 所以你可以使你的函数接受一个Object数组:

public void func(Object[] args) {
}

Or if you want to be able to pass nothing: 或者如果你想要什么都不通过:

public void func(Object... args) {
}
public void omnivore(Object... args) {
   // what now?
}

In Java, a variable of any reference type (objects and arrays), including ones of some generic type, even wildcards, can be passed to a parameter of type Object. 在Java中,任何引用类型(对象和数组)的变量(包括某些泛型类型的变量,甚至通配符)都可以传递给Object类型的参数。 A variable of any primitive type can be autoboxed to its corresponding wrapper type, which is a reference type, and so can be passed as Object. 任何基本类型的变量都可以自动装箱到其相应的包装类型,这是一种引用类型,因此可以作为Object传递。 So, Object... will accept any number of anything. 因此, Object...将接受任何数量的任何东西。

Use this syntax: 使用以下语法:

void myMethod(Object... args) {
    // Here, args is an array of java.lang.Object:
    // you can take its length, get its elements with [i] operator,
    // and so on.
}

The closest you will get is someMethod(Object ... args) . 你得到的最接近的是someMethod(Object ... args)

Strictly, this does not accept all argument types. 严格来说,这不接受所有参数类型。 Specifically, it does not accept primitive types: these need boxed to the corresponding wrapper types. 具体来说,它不接受原始类型:这些需要装入相应的包装类型。 Normally this makes no difference. 通常这没有区别。 But it does if you need to distinguish between primitive and wrapper types in the called method. 但是如果你需要区分被调用方法中的原始类型和包装类型,它就会这样做。

暂无
暂无

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

相关问题 如何制作接受任何类型变量的 Java 函数? - How can I make a Java function that accepts any type of variable? 如何使接受任何对象作为参数的JAVA方法? - How to make JAVA method that accepts any object as parameter? 是否有可能创建一个接受任意数量参数的方法? - Is it possible to make a method that takes any number of arguments? Java中是否有任何KV数据结构可以接受原始类型的密钥? - Is there any KV data structure in Java which accepts key in primitive type? 如何创建接受可变数量参数的 Java 方法? - How can I create a Java method that accepts a variable number of arguments? @Autowired任何带有任意数量参数的方法名称 - @Autowired any method name with any number of arguments 如何从用户那里获取任意数量的参数并将其传递给使用varargs(variable arguments)的方法? - How to take any number of arguments from the user and pass them to a method which uses varargs( variable argument )? 如何创建一个接受任何数据类型的队列? - How to create a Queue which accepts any Datatype? 如何使用接受任何类型的 ArrayList 的泛型方法并在指定类型的类中使用该方法? - How to use generic method that accepts an ArrayList of any type and use the method in the specified type class? 如何编写一个接受任何扩展Throwable类的集合的方法? - How to write a method which accepts any collection of classes which extend Throwable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM