日期:2014-05-20  浏览次数:20769 次

求高效代码
举个例子:求高效判断事件源的代码
方法1.
actionListener( ActionEvent e)
{
  Compoent c=(Component) e.getSource()
  if(instance==c) //随便一个对象instance
  {

  }
  else if(instance==c)
  {}
  .......
}
方法2.
actionListener( ActionEvent e)
{
  Compoent c=(Component) e.getSource()
  if(instance==c) //随便一个对象instance
  {
  expression;
  return;
  }
  if(instance==c)
  {
  expression;
  return;

  }
  .....
 }
方法3. 判断语句为
List<Component> listenedList;//被监听对象的集合

  int id= listenedList.indexOf(e.getID())
  switch(id)
  {
  case 0:express;break;
  case 1:....
  }
   

求高手评下那种方法更高效 有比这更高效的吗共享下谢谢了大虾!!!!!!!!






------解决方案--------------------
怎么说呢,性能这个指标不是通过这种方式体现的. 很多代码最终都会被编译器优化的.

第三种没看懂.
------解决方案--------------------
方法一吧

首先if 比 switch快

其次 if(){} else if(){}else{} 比 if(){} if(){} if(){} 快