求高效代码

tiantiandrinksjava 2012-03-13 02:20:15
举个例子:求高效判断事件源的代码
方法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:....
}


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




...全文
71 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
菖蒲老先生 2012-03-13
  • 打赏
  • 举报
回复
方法一吧

首先if 比 switch快

其次 if(){} else if(){}else{} 比 if(){} if(){} if(){} 快
tiantiandrinksjava 2012-03-13
  • 打赏
  • 举报
回复
listenedList 被监听对象的集合
e.getSource()
listenedList.indexOf(e.getSource())获取事件源的在集合中的索引
不好意思写错成 e.getID()了
orangemike 2012-03-13
  • 打赏
  • 举报
回复
怎么说呢,性能这个指标不是通过这种方式体现的. 很多代码最终都会被编译器优化的.

第三种没看懂.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧