- package com.cw.ubuntu.test;
- public class TestForeach {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //同类型不同数量参数的重载的写法
- System.out.println("No argument");
- fun();
- System.out.println("One argument");
- fun(1);
- System.out.println("Two argument");
- fun(1,2);
- System.out.println("Three argument");
- fun(1,2,3);
- //此处说明==的优先级比++大
- int t = 2;
- int x = 1;
- System.out.println(t == x++);
- System.out.println(x);
- }
- public static void fun(int...args) {
- // TODO Auto-generated method stub
- int temp = 1;
- for (int i : args){
- if(args.length == temp++)
- System.out.print(i);
- else
- System.out.print(i + ", ");
- }
- System.out.println("\nthe num of the arguments are :" + args.length);
- }
- }
复制代码 输出结果:
No argument
the num of the arguments are :0
One argument
1
the num of the arguments are :1
Two argument
1, 2
the num of the arguments are :2
Three argument
1, 2, 3
the num of the arguments are :3
false
2
综上:
1.同类型不同数量的参数,重载可考虑使用
2.附加,在输出地方针对最后一个元素作了格式优化,发现==的优先级高于++
|