免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1829 | 回复: 2
打印 上一主题 下一主题

初始化有序常量的有什么技巧 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-24 14:33 |只看该作者 |倒序浏览


初始化有序常量的有什么技巧?如何对一个常见的情况是对命令行参数进行处理,如下的代码片段:
  

 public class A {   
   
  private static final int PARAM_1 = 0;   
   
  private static final int PARAM_2 = 1;   
   
  private static final int PARAM_3 = 2;   
   
  private static final int PARAM_4 = 3;   
   
  private static final int PARAMS_COUNT = 4;   
 public class A {

  private static final int PARAM_1 = 0;

  private static final int PARAM_2 = 1;

  private static final int PARAM_3 = 2;

  private static final int PARAM_4 = 3;

  private static final int PARAMS_COUNT = 4;

  …….   


public static void main(String[] args) {   
   
  if (args.length != PARAMS_COUNT) {   
   
  System.out.println("Usage: program <aram_1> <aram_2> <aram_3> <aram_3> <aram_4>";   
   
  System.exit(1);   
   
  }   
   
  callFun1(args[PARAM_1], args[PARAM_2]);   
   
  callFun2(args[PARAM_1], args[PARAM_3], args[PARAM_4]);   
   
  }   
   
  }   
public static void main(String[] args) {

  if (args.length != PARAMS_COUNT) {

  System.out.println("Usage: program <aram_1> <aram_2> <aram_3> <aram_3> <aram_4>";

  System.exit(1);

  }

  callFun1(args[PARAM_1], args[PARAM_2]);

  callFun2(args[PARAM_1], args[PARAM_3], args[PARAM_4]);

  }

  }

  PARAM_1到PARAM_4就是我这里说的有序产量,这样的常量通常被赋予一段顺序的值。但如果像上面这段代码的写法,将会在程序维护时带来一些小麻烦。比如增加一个新的产量PARAM_5,除了需要定义型的常量以外,还需要调整PARAMS_COUNT的值。如果需要在原序列中插入一个新的常量,那就更麻烦了,不仅需要调整PARAMS_COUNT的值,还需要调整插入位置之后的常量的值。如在上面的代码中插入一个常量PARAM_NEW到PARAM_3的位置:    


private static final int PARAM_1 = 0;   
   
  private static final int PARAM_2 = 1;   
   
  private static final int PARAM_NEW = 2      // New Constant   
   
  private static final int PARAM_3 = 3;      // Need Changed   
   
  private static final int PARAM_4 = 4;      // Need Changed   
   
  private static final int PARAMS_COUNT = 5;  // Need Changed   
private static final int PARAM_1 = 0;

  private static final int PARAM_2 = 1;

  private static final int PARAM_NEW = 2      // New Constant

  private static final int PARAM_3 = 3;      // Need Changed

  private static final int PARAM_4 = 4;      // Need Changed

  private static final int PARAMS_COUNT = 5;  // Need Changed
  



其实我们只需要引入一个静态变量就可以比较好的解决这个问题。下面是修改后的代码:    


public class A {   
   
  private static int PARAM_INDEX = 0;   
   
  private static final int PARAM_1 = PARAM_INDEX++;   
   
  private static final int PARAM_2 = PARAM_INDEX++;   
   
  private static final int PARAM_3 = PARAM_INDEX++;   
   
  private static final int PARAM_4 = PARAM_INDEX++;   
   
  private static final int PARAMS_COUNT = PARAM_INDEX;   
   
   
  }   
public class A {

  private static int PARAM_INDEX = 0;

  private static final int PARAM_1 = PARAM_INDEX++;

  private static final int PARAM_2 = PARAM_INDEX++;

  private static final int PARAM_3 = PARAM_INDEX++;

  private static final int PARAM_4 = PARAM_INDEX++;

  private static final int PARAMS_COUNT = PARAM_INDEX;


  }

  在新的代码中,我们引入了一个变量PARAM_INDEX,并赋予一个初始值(这里是零),借助于对这个变量,我们消除了硬编码常量的值带来的那些弊端。同样插入一个新的常量,现在我们只需要增加新的内容,而不需要改变原有的代码了。代码如下:    


public class A {   
   
    
  private static int PARAM_INDEX = 0;   
   
  private static final int PARAM_1 = PARAM_INDEX++;   
   
  private static final int PARAM_2 = PARAM_INDEX++;   
   
  private static final int PARAM_NEW = PARAM_INDEX++;  // New Constant   
   
  private static final int PARAM_3 = PARAM_INDEX++;   
   
  private static final int PARAM_4 = PARAM_INDEX++;   
   
  private static final int PARAMS_COUNT = PARAM_INDEX;   
   
  }  
原文请查看中睿it服务:http://www.itlead.com.cn/article/html/268/2012-04-23/content-8441.shtml



论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2012-04-24 14:54 |只看该作者
广告帖子~~

论坛徽章:
0
3 [报告]
发表于 2012-05-07 22:21 |只看该作者
好,应该顶,今后继续努力
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP