A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ZYZQ 中级黑马   /  2015-6-1 23:58  /  289 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.cw.ubuntu.test;

  2. public class TestForeach {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 //同类型不同数量参数的重载的写法
  9.                 System.out.println("No argument");
  10.                 fun();
  11.                 System.out.println("One argument");
  12.                 fun(1);               
  13.                 System.out.println("Two argument");
  14.                 fun(1,2);
  15.                 System.out.println("Three argument");
  16.                 fun(1,2,3);
  17.                 //此处说明==的优先级比++大
  18.                 int t = 2;
  19.                 int x = 1;
  20.                 System.out.println(t == x++);
  21.                 System.out.println(x);
  22.         }

  23.         public static void fun(int...args) {
  24.                 // TODO Auto-generated method stub
  25.                 int temp = 1;
  26.                 for (int i : args){
  27.                         if(args.length == temp++)
  28.                                 System.out.print(i);
  29.                         else
  30.                                 System.out.print(i + ", ");
  31.                 }
  32.                 System.out.println("\nthe num of the arguments are :" + args.length);
  33.         }

  34. }
复制代码
输出结果:
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.附加,在输出地方针对最后一个元素作了格式优化,发现==的优先级高于++

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马