当前位置:网站首页>接口作为参数(接口回调)
接口作为参数(接口回调)
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); 动物接口找到猫的对象地址,调用猫的吃东西方法
边栏推荐
- 【踩坑系列】uniapp之h5 跨域的问题
- Complex network modeling (III)
- Réplication de vulnérabilité - désrialisation fastjson
- Caractéristiques de bisenet
- offer收割机:两个长字符串数字相加求和(经典面试算法题)
- Four items that should be included in the management system of integral mall
- Summary of redis functions
- 【无标题】
- 提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
- Myabtis_Plus
猜你喜欢
buureservewp(2)
王爽 《汇编语言》之寄存器
电池、电机技术受到很大关注,反而电控技术却很少被提及?
CDC (change data capture technology), a powerful tool for real-time database synchronization
JS copy picture to clipboard read clipboard
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
在Rainbond中实现数据库结构自动化升级
LeetCode中等题之我的日程安排表 I
使用 Nocalhost 开发 Rainbond 上的微服务应用
通俗易懂单点登录SSO
随机推荐
Interview questions (CAS)
复杂网络建模(二)
青龙面板--整理能用脚本
面试题(CAS)
Niu Mei's mathematical problem --- combinatorial number
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
Function extension, attribute extension and non empty type extension in kotlin
【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
Fast parsing intranet penetration escorts the document encryption industry
Topic not received? Try this
电池、电机技术受到很大关注,反而电控技术却很少被提及?
Application of slip ring of shipborne radar antenna
Use of out covariance and in inversion in kotlin
Make LIVELINK's initial pose consistent with that of the mobile capture actor
Blob 对象介绍
JS cross browser parsing XML application
UnityHub破解&Unity破解
Find the mode in the binary search tree (use medium order traversal as an ordered array)
The element with setfieldsvalue set is obtained as undefined with GetFieldValue
Wang Zijian: is the NFT of Tencent magic core worth buying?