[英]How to declare multiple parameters with the same data type? (java)
When declaring multiple variables, I can just use this syntax声明多个变量时,我可以使用这种语法
int a, b, c;
But since the comma ,
is used to separate deceleration of parameters of the method, I can't use the above syntax但是由于逗号
,
是用来分隔方法参数的减速,所以我不能使用上面的语法
public void method (int a, int b, int c) { }
So, how can I declare multiple parameters without typing the same data type multiple times?那么,如何在不多次输入相同数据类型的情况下声明多个参数?
You could try a constructor?你可以试试构造函数? Something like:
就像是:
public ExampleConstructor(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
Then in your method just pass the constructor variables.然后在你的方法中只传递构造函数变量。 You can use getters in your method to fetch the desired variable:
您可以在方法中使用 getter 来获取所需的变量:
public void method (ExampleConstructor eC)
{
whateverMethodIsDoing = eC.getA;
}
This may be my first response to a question at stackOverflow, I hope it is useful.这可能是我在 stackOverflow 上对一个问题的第一次回答,我希望它有用。
try below method.试试下面的方法。
public void method(int... intVar){
for(int num: intVar){
System.out.println(num);
}
}
and和
method(1,2,3,4,5);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.