可变参数所在的方法是最后被访问的
代码如下
package test;
public class Test {
public static void main(String[] args) {
Test test = new Test();
System.out.println("和为:" + test.plus(4, 6));
}
static void method8() {
String s1 = new String("bbb");
String s2 = new String("bbb");
if (s1 == s2) {
System.out.println("ok");
} else {
System.out.println("no");
}
if (s1.equals(s2)) {
System.out.println("OK");
} else {
System.out.println("NO");
}
}
public int plus(int a, int b) {
System.out.println("不带可变参数的方法被调用!");
return a + b;
}
public int plus(int a, int... b) {
System.out.println("带可变参数的方法被调用!");
int sum = 0;
for (int n : b) {
sum += n;
}
return sum;
}
}
运行结果如图: