The method is public static void method(Object[] params)
, how should I call it in the following scenarios?
这个方法是public static void方法(Object[] params),我应该如何在以下场景中调用它?
- with one object as parameter
ClassA a
- 一个对象作为参数ClassA
- with more than one objects as parameters
ClassA a
,ClassB b
,ClassC c
? thank you - 有多个对象作为参数ClassA, ClassB b, ClassC c?谢谢你!
1 个解决方案
#1
27
You can create the array of objects on the fly:
您可以动态创建对象数组:
method(new Object[] { a, b, c});
Another suggestion is that you change the signature of the method so that it uses java varargs:
另一个建议是修改方法的签名,使其使用java varargs:
public static void method(Object... params)
Nice thing is that it is compiled into a method with the same signature as above (Object[] params)
. But it may be called like method(a)
or method(a, b, c)
.
很好的一点是,它被编译成具有与上面相同签名的方法(Object[] params)。但是它可以被称为方法(a)或方法(a, b, c)。
#1
27
You can create the array of objects on the fly:
您可以动态创建对象数组:
method(new Object[] { a, b, c});
Another suggestion is that you change the signature of the method so that it uses java varargs:
另一个建议是修改方法的签名,使其使用java varargs:
public static void method(Object... params)
Nice thing is that it is compiled into a method with the same signature as above (Object[] params)
. But it may be called like method(a)
or method(a, b, c)
.
很好的一点是,它被编译成具有与上面相同签名的方法(Object[] params)。但是它可以被称为方法(a)或方法(a, b, c)。