专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Javascript教程 » java回调函数:java和javascript中的回调函数 »正文

java回调函数:java和javascript中的回调函数

来源: 发布时间:星期四, 2009年1月15日 浏览:19次 评论:0
="t18">先看看java中回调java中很多模式和它类似有访问者观察者等模式ioc等也有这个类似原理





Java代码
public Test {

/**
* @param args
*/

public void (String args) {
// TODO Auto-generated method stub
Test.doExecute( CallBack{

public void execute {
.out.prln("正在执行....");

}

});
}

private void doExecute(CallBack callBack) {

.out.prln("回调前.....");
callBack.execute;
.out.prln("回调后.....");
}

}

public Test {

/**
* @param args
*/

public void (String args) {
// TODO Auto-generated method stub
Test.doExecute( CallBack{

public void execute {
.out.prln("正在执行....");

}

});
}

private void doExecute(CallBack callBack) {

.out.prln("回调前.....");
callBack.execute;
.out.prln("回调后.....");
}

} 输出结果为:





Java代码
回调前.....
正在执行....
回调后.....

回调前.....
正在执行....
回调后.....这里充分体现了不是我来而是你来

这里我就是“doExecute”你就是"execute"





回调可以用在哪些地方呢?



1比如著名ioc事务处理就是这样每个思路方法事务处理都是都是些开始个事务用try包住等等所以就可以不管他们只是要执行思路方法区别比如存储删除查询什么



2可以减少写类个接口可以有很多实现类在回调里面就成了匿名著名jive里面DatabaseObjectFactory 就是这个使用方法



Java代码
erface DatabaseObjectFactory {

/**
* Returns the object associated with id or null the
* object could not be loaded.
*
* @param id the id of the object to load.
* @ the object specied by id or null it could not
* be loaded.
*/
public Object loadObject(long id);
}

erface DatabaseObjectFactory {

/**
* Returns the object associated with id or null the
* object could not be loaded.
*
* @param id the id of the object to load.
* @ the object specied by id or null it could not
* be loaded.
*/
public Object loadObject(long id);
}


外面根据id区别产生任何你想要对象



在看看javascript里面:







Js代码
function Test
{
var self=this;
this.func1=function
{
self.test2(function { alert('正在执行');})
}
}

Test.prototype.test2=function(func)
{
alert("回调前做些东西");
(func)
func;
alert("回调后做些东西");
}

function Test
{
var self=this;
this.func1=function
{
self.test2(function { alert('正在执行');})
}
}

Test.prototype.test2=function(func)
{
alert("回调前做些东西");
(func)
func;
alert("回调后做些东西");
}
然后在html里面:

Html代码
<html>
<head>
<script type=text/javascript src="1.js">
</script>
<script>
Test.func1;
</script>
</head>
<body>
</body>
</html>

<html>
<head>
<script type=text/javascript src="1.js">
</script>
<script>
Test.func1;
</script>
</head>
<body>
</body>
</html>
java和javascript回调都差不多

声明:JavaEye文章版权属于作者受法律保护没有作者书面许可不得转载

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: