当前位置:网站首页>接口作为参数(接口回调)
接口作为参数(接口回调)
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); 动物接口找到猫的对象地址,调用猫的吃东西方法
边栏推荐
- Network learning (I) -- basic model learning
- 【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
- 在Rainbond中实现数据库结构自动化升级
- The largest 3 same digits in the string of leetcode simple question
- Interactive book delivery - signed version of Oracle DBA work notes
- Avatary的LiveDriver试用体验
- ZCMU--1492: Problem D(C语言)
- JS cross browser parsing XML application
- Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
- Uniapp mobile terminal forced update function
猜你喜欢
船载雷达天线滑环的使用
漏洞复现-easy_tornado
opencv学习笔记四——膨胀/腐蚀/开运算/闭运算
Record a stroke skin bone error of the skirt
Real time monitoring of dog walking and rope pulling AI recognition helps smart city
Basic use of CTF web shrink template injection nmap
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
Réplication de vulnérabilité - désrialisation fastjson
藏书馆App基于Rainbond实现云原生DevOps的实践
Jmeter 的使用
随机推荐
The use of generics and vararg variable parameters in kotlin
解析机器人科技发展观对社会研究论
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
Hisense TV starts the developer mode
Blob 對象介紹
OpenVSCode云端IDE加入Rainbond一体化开发体系
Offer harvester: add and sum two long string numbers (classic interview algorithm question)
CDC (change data capture technology), a powerful tool for real-time database synchronization
eBPF Cilium实战(2) - 底层网络可观测性
buureservewp(2)
JS copy picture to clipboard read clipboard
Avatary's livedriver trial experience
复杂网络建模(二)
It took "7" years to build the robot framework into a micro service
[quick start of Digital IC Verification] 12. Introduction to SystemVerilog testbench (svtb)
Detailed explanation of apply, also, let, run functions and principle analysis of internal source code in kotlin
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
Make LIVELINK's initial pose consistent with that of the mobile capture actor
[quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)