回调函数callback,是指通过函数传递参数传递到其他代码,某一块可执行的代码引用。
废话不多说先看一段代码实例。
/**
* @author keying
* @date 2022-12-05 20:07:05
*/
public class Boss221205 implements CallbackInterface {
private Employee221205 employee221205;
public Boss221205(Employee221205 employee221205){
this.employee221205 = employee221205;
}
public void question(){
employee221205.question(this);
}
@Override
public void callbackMethod() {
}
@Override
public void tellBoss(String answer) {
System.out.println("回答boss:" + answer);
}
}
/**
* @author keying
* @date 2022-12-05 19:53:35
*/
public interface CallbackInterface {
public void callbackMethod();
public void tellBoss(String answer);
}
/**
* @author keying
* @date 2022-12-05 20:08:14
*/
public interface Employee221205 {
public void question(Boss221205 boss221205);
}
/**
* @author keying
* @date 2022-12-05 20:11:43
*/
public class EmployeeA221205 implements Employee221205{
@Override
public void question(Boss221205 boss221205) {
try{
Thread.sleep(3000);
}catch (Exception e){
}
// 回调
boss221205.tellBoss("三秒后回调answer");
}
}
/**
* @author keying
* @date 2022-12-05 19:55:02
*/
public class Main20221205 {
public static void main(String[] args) {
Employee221205 employeeA221205 = new EmployeeA221205();
Boss221205 boss221205 = new Boss221205(employeeA221205);
boss221205.question();
}
}
除了一些函数存在固定代码以外,还有一部分代码根据实际业务来写不同的代码,而这种业务场景最适合用回调函数来替代。回调函数如同占位符号,代码由调用者传递执行,回调函数提升函数重用率。
/**
* @author keying
* @date 2022-12-05 21:31:47
*/
public interface Callback1205 {
void append(FileWriter fileWriter);
}
/**
* @author keying
* @date 2022-12-05 21:33:10
*/
public class Write20221205 {
public void write(String filename, Callback1205 callback1205) {
FileWriter fileWriter;
try{
fileWriter = new FileWriter(filename,true);
callback1205.append(fileWriter);
fileWriter.flush();
fileWriter.close();
}catch (Exception e){
}
}
}
/**
* @author keying
* @date 2022-12-05 21:35:22
*/
public class WriteMain20221205 {
public static void main(String[] args) {
Write20221205 write20221205 = new Write20221205();
write20221205.write("D:\\fw.txt", fileWriter -> {
try{
fileWriter.write("write 20221205");
}catch (Exception e){
}
});
/* Write20221205 write20221205 = new Write20221205();
write20221205.write("D:\\fw.txt", new Callback1205() {
@Override
public void append(FileWriter fileWriter) {
try{
fileWriter.write("回调");
}catch (Exception e){
}
}
});*/
}
}
先来一段lambda简写形式:上面代码可以看到,我们可以通过回调函数来实现自己需要write什么内容,这样写增加了代码重用率,在A函数里通过书写函数b的函数名来调用。按照定义好的接口规范,来供其他函数调用。使用技巧则是定义一个接口,在接口中定义我们想回调的方法。
在写个监听按钮的回调函数加深印象:
1、我们先定义一个监听按钮接口。
2、定义点击类,然后main方法调用点击。
/**
* @author keying
* @date 2022-12-05 22:28:54
*/
public interface ButtonListener {
void listener();
}
/**
* @author keying
* @date 2022-12-05 22:29:50
*/
public class ButtonMain1205 {
public static void main(String[] args) {
ClickButton clickButton = new ClickButton();
clickButton.click();
}
}
/**
* @author keying
* @date 2022-12-05 22:29:19
*/
public class ClickButton {
public void click(){
System.out.println("点击成功");
}
}
这时候已经点击成功了,那么如何通过回调函数监听这个点击动作呢。
public static void main(String[] args) {
ClickButton clickButton = new ClickButton();
clickButton.setButtonListener(new ButtonListener() {
@Override
public void listener() {
System.out.println("触发点击动作");
}
});
clickButton.click();
}
private ButtonListener buttonListener;
public void setButtonListener(ButtonListener buttonListener){
this.buttonListener = buttonListener;
}
public void click(){
if(buttonListener!=null){
buttonListener.listener();
}
System.out.println("点击成功");
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有