Java中的接口回调实例
作者:Moment°回忆 时间:2023-11-29 08:05:43
定义:
/**
* @author Administrator
* @project: TestOne
* @package: PACKAGE_NAME
* @date: 2018/11/30 0030 15:42
* @brief: 郭宝
**/
public class Person {
/**
* 自定义一个接口
**/
public interface OnNameChangeListener{
//接口中的抽象函数,并携带数据
void onNameChange(String name);
}
/**
* @param name
* @param onNameChangeListener 传入实现了该接口的子类对象
**/
public void setName(String name,OnNameChangeListener onNameChangeListener){
if (onNameChangeListener!=null){
//调用接口中的抽象函数
onNameChangeListener.onNameChange("你好,"+name);
}
}
}
调用:
/**
* @author Administrator
* @project: TestOne
* @package: PACKAGE_NAME
* @date: 2018/11/30 0030 15:39
* @brief: 郭宝
**/
public class mian {
public static void main(String[] a){
//实例化Person,并设置名称,传入实现OnNameChangeListener接口的子类对象
new Person().setName("郭宝", new Person.OnNameChangeListener() {
@Override
public void onNameChange(String name) {
System.out.println(name);
//你好,郭宝
}
});
}
}
来源:https://blog.csdn.net/qq_35366269/article/details/84655982
标签:java,接口回调
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# ManualResetEvent用法详解
2021-06-11 09:37:14
![](https://img.aspxhome.com/file/2023/1/113811_0s.png)
java实现波雷费密码算法示例代码
2022-09-14 23:30:28
![](https://img.aspxhome.com/file/2023/3/62073_0s.png)
Linux下java环境配置图文方法
2021-10-24 06:22:52
![](https://img.aspxhome.com/file/2023/0/70300_0s.png)
Java注解(annotation)简述
2022-03-07 08:10:12
![](https://img.aspxhome.com/file/2023/4/62824_0s.png)
Android异步消息机制详解
2023-08-07 09:42:52
![](https://img.aspxhome.com/file/2023/0/85960_0s.png)
解析C#中@符号的几种使用方法详解
2022-11-09 03:26:03
Java Gradle项目中的资源正确获取方式
2022-10-05 09:00:50
![](https://img.aspxhome.com/file/2023/8/70568_0s.jpg)
Android使用剪切板传递数据
2022-06-07 05:44:28
Spring超详细讲解面向对象到面向切面
2021-12-28 15:15:57
![](https://img.aspxhome.com/file/2023/2/89472_0s.png)
mybatis的大于小于号转义符号一览
2023-05-31 07:23:03
![](https://img.aspxhome.com/file/2023/7/60667_0s.png)
解决SpringMVC使用@RequestBody注解报400错误的问题
2022-02-26 16:06:43
再谈java回调函数
2023-09-27 19:20:10
ElasticSearch添加索引代码实例解析
2023-11-21 03:41:04
![](https://img.aspxhome.com/file/2023/3/59543_0s.png)
java项目中使用 Lombok遇到的问题小结
2023-09-11 02:09:53
![](https://img.aspxhome.com/file/2023/7/69937_0s.png)
c#快速写本地日志方法
2021-08-24 09:31:17
![](https://img.aspxhome.com/file/2023/2/106382_0s.png)
C#操作注册表的方法
2022-04-14 03:20:36
解决Java中properties文件编码问题
2022-07-21 14:09:06
![](https://img.aspxhome.com/file/2023/8/64218_0s.jpg)
android 权限大全 分享
2023-03-30 18:06:22
C#使用System.Net邮件发送功能踩过的坑
2022-09-09 18:16:11
![](https://img.aspxhome.com/file/2023/3/113693_0s.png)
Java NIO和IO的区别
2023-07-15 22:53:46
![](https://img.aspxhome.com/file/2023/2/75432_0s.png)