简体   繁体   English

是否有可能创建一个接受任意数量参数的方法?

[英]Is it possible to make a method that takes any number of arguments?

I was looking at another post here which was describing what seems to be my problem : How to make a method which accepts any number of arguments of any type in Java? 我在看这里的另一个帖子,它描述我的 问题如何创建一个接受Java中任何类型的任意数量的参数的方法?

However , when I attempted to do this method, when I compiled the program it gave me the error "int cannot be converted to java.util.Objects" 但是 ,当我尝试执行此方法时,当我编译程序时它给了我错误 “int无法转换为java.util.Objects”

What am I doing wrong? 我究竟做错了什么?

Code: 码:

public static void clearArray (Objects... args)
{
    System.out.println("Error, non character value");
}

How I called the function: 我怎么称呼函数:

import java.util.Objects;
// Stuff...
clearArray(1);
// Other stuff...

Thank you for checking out my problem! 感谢您查看我的问题!

Look at the signature 看看签名

public static void clearArray (Objects... args)

That method receivng Objects type and you are passing integer to it. 该方法接收对象类型,并将整数传递给它。 Perhaps changing that to 也许改变它

public static void clearArray (Object... args)

Helps. 帮助。

You want java.lang.Object , not java.util.Objects . 你想要java.lang.Object ,而不是java.util.Objects

java.util.Objects is a class with utility methods, not a class you can actually extend and instantiate. java.util.Objects是一个带有实用程序方法的类,而不是可以实际扩展和实例化的类。

java.lang.Object on the other hand is the superclass of all objects in Java. 另一方面, java.lang.Object是Java中所有对象的超类。

And even in a multi-param (varargs) method, the signature needs to be Object ... , not Objects ... . 即使在多参数(varargs)方法中,签名也需要是Object ... ,而不是Objects ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM