当前位置:网站首页>接口作为参数(接口回调)
接口作为参数(接口回调)
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); 动物接口找到猫的对象地址,调用猫的吃东西方法
边栏推荐
- Openjudge noi 2.1 1752: chicken and rabbit in the same cage
- Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
- 机器人教育在动手实践中的真理
- Register of assembly language by Wang Shuang
- LeetCode中等题之我的日程安排表 I
- UnityHub破解&Unity破解
- 【无标题】
- The element with setfieldsvalue set is obtained as undefined with GetFieldValue
- eBPF Cilium实战(1) - 基于团队的网络隔离
- 漏洞复现-Fastjson 反序列化
猜你喜欢
JS copy picture to clipboard read clipboard
电池、电机技术受到很大关注,反而电控技术却很少被提及?
Niu Mei's mathematical problem --- combinatorial number
饥荒云服管理脚本
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
Bisenet features
柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
漏洞复现-easy_tornado
Myabtis_ Plus
Real time monitoring of dog walking and rope pulling AI recognition helps smart city
随机推荐
快解析内网穿透助力外贸管理行业应对多种挑战
电池、电机技术受到很大关注,反而电控技术却很少被提及?
Interactive book delivery - signed version of Oracle DBA work notes
[quick start of Digital IC Verification] 12. Introduction to SystemVerilog testbench (svtb)
The reified keyword in kotlin is used for generics
It took "7" years to build the robot framework into a micro service
CTF-WEB shrine模板注入nmap的基本使用
发挥创客教育空间的广泛实用性
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
积分商城管理系统中应包含的四大项
Qinglong panel - today's headlines
一文了解如何源码编译Rainbond基础组件
Caractéristiques de bisenet
OpenJudge NOI 2.1 1752:鸡兔同笼
[untitled]
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
Understanding of out covariance, in inversion and invariance in kotlin
Summary of redis functions
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
Interview questions (CAS)