---
title: "java中的回调函数"
author: "vortexchoo"
date: "2015-02-09T09:34:33Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2015-02-09T09:34:33Z"
id: 2184503
url: "https://vortexchoo.iteye.com/blog/2184503"
---

用惯了ajax，喜欢上了回调函数。所以写了个小例子实现了下java 中的回调

 

```
public interface CallBackInterface {
	
	public void doCallBack(String str);
	
}



public class CallBackMethod {
	
	public void doCallBack(String str,CallBackInterface call){
		//加工传入参数并得到最终结果res
		String res = str + "callBack";
		//将res传入接口
		call.doCallBack(res);
	}
	
}




public class Test {
	
	public static void main(String[] args) {
		new CallBackMethod().doCallBack("show ", new CallBackInterface(){
			@Override
			public void doCallBack(String str) {
				// TODO Auto-generated method stub
				System.out.println(str);
			}
			
		});
	}
}
```

 通过回调函数，我们拿到了加工后的值。