当前位置:网站首页>接口作为参数(接口回调)
接口作为参数(接口回调)
2022-07-07 05:20:00 【寒_川】
interface Animal {
void eat();
}
@Slf4j
class Cat implements Animal {
@Override
public void eat() {
log.debug("猫吃鱼");
}
}
@Slf4j
class Dog implements Animal {
@Override
public void eat() {
log.debug("狗吃肉");
}
}
class AnimalTest {
//接口作为参数
public void AnimalEat(Animal animal) {
animal.eat();
}
}
实现
@Test
public void interfaceCallbackTest(){
Animal1 cat = new Cat(); //Cat类实现了Animal1,则Animal1指向Cat类的对象地址
Animal2 dog = new Dog(); //Dog类实现了Animal2,则Animal2指向Dog类的对象地址
AnimalTest animalTest = new AnimalTest();
animalTest.AnimalEat(cat); // 实际上调用的是cat的eat()方法 输出猫吃鱼
animalTest.AnimalEat(dog); // 实际上调用的是dog的eat()方法 输出狗吃肉
}
Animal1 cat = new Cat(); 所谓的接口回调,即 哪个类实现了他,他就指向哪个类的对象地址
animalTest.AnimalEat(cat); 动物接口找到猫的对象地址,调用猫的吃东西方法
边栏推荐
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- Empire CMS collection Empire template program general
- ZCMU--1492: Problem D(C语言)
- Summary of redis functions
- 漏洞复现-Fastjson 反序列化
- Hisense TV starts the developer mode
- Call pytorch API to complete linear regression
- CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
- [quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
- opencv学习笔记三——图像平滑/去噪处理
猜你喜欢

Make LIVELINK's initial pose consistent with that of the mobile capture actor

Réplication de vulnérabilité - désrialisation fastjson

Unityhub cracking & unity cracking

LeetCode中等题之我的日程安排表 I

电池、电机技术受到很大关注,反而电控技术却很少被提及?

LeetCode简单题之找到一个数字的 K 美丽值

The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number

数据库实时同步利器——CDC(变化数据捕获技术)

CDC (change data capture technology), a powerful tool for real-time database synchronization

Myabtis_ Plus
随机推荐
一文了解如何源码编译Rainbond基础组件
Leetcode medium question my schedule I
Register of assembly language by Wang Shuang
Fast parsing intranet penetration escorts the document encryption industry
柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
The reified keyword in kotlin is used for generics
面试题(CAS)
OpenJudge NOI 2.1 1752:鸡兔同笼
Qinglong panel -- Huahua reading
Empire CMS collection Empire template program general
ZCMU--1492: Problem D(C语言)
青龙面板-今日头条
Full text query classification
uniapp 移动端强制更新功能
Use of out covariance and in inversion in kotlin
机器人教育在动手实践中的真理
Vulnerability recurrence easy_ tornado
[untitled]
Kotlin combines flatmap for filtering and zip merge operators
JS复制图片到剪切板 读取剪切板