当前位置:网站首页>(2)接口中新增的方法
(2)接口中新增的方法
2022-07-03 09:15:00 【look-word】
接口中新增的方法
1. JDK8中接口的新增
在JDK8中针对接口有做增强,在JDK8之前
interface 接口名{
静态常量;
抽象方法;
}
JDK8之后对接口做了增加,接口中可以有默认方法和静态方法
interface 接口名{
静态常量;
抽象方法;
默认方法;
静态方法;
}
2 默认方法
2.1 为什么要增加默认方法
在JDK8以前接口中只能有抽象方法和静态常量,会存在以下的问题:
如果接口中新增抽象方法,那么实现类都必须要抽象这个抽象方法,非常不利于接口的扩展的接口中新增抽象方法,所有实现类都需要重写这个方法,不利于接口的扩展
2.2 接口默认方法的格式
接口中默认方法的语法格式是
interface 接口名{
修饰符 default 返回值类型 方法名{
方法体;
}
}
2.3 接口中默认方法的使用
接口中的默认方法有两种使用方式
- 实现类直接调用接口的默认方法
- 实现类重写接口的默认方法
3. 静态方法
JDK8中为接口新增了静态方法,作用也是为了接口的扩展
3.1 语法规则
interface 接口名{
修饰符 static 返回值类型 方法名{
方法体;
}
}
3.2 静态方法的使用
接口中的静态方法在实现类中是不能被重写的,调用的话只能通过接口类型来实现: 接口名.静态方法名();
想信小伙伴们会有这种疑问 那接口中的静态方法能不能被继承呢 就是类实现接口 还能不能调用接口中的静态方法呢
4两者的区别介绍
- 默认方法通过实例调用,静态方法通过接口名调用
- 默认方法可以被继承,实现类可以直接调用接口默认方法,也可以重写接口默认方法
- 静态方法不能被继承,实现类不能重写接口的静态方法,只能使用接口名调用
边栏推荐
- NR technology -- MIMO
- Characteristics of PUCCH formats
- JS基础-原型原型链和宏任务/微任务/事件机制
- 2020-08-23
- You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
- [CSDN] C1 training problem analysis_ Part IV_ Advanced web
- Leetcode daily question (2090. K radius subarray averages)
- Flink CDC practice (including practical steps and screenshots)
- Schematic diagram and connection method of six pin self-locking switch
- 顺利毕业[3]-博客系统 更新中。。。
猜你喜欢
IDEA远程断点调试jar包项目
Electronic product design
CEF下载,编译工程
STM32 port multiplexing and remapping
顺利毕业[3]-博客系统 更新中。。。
Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
Development of fire evacuation system
MYSQL数据库底层基础专栏
SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
随机推荐
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
Project scope management__ Scope management plan and scope specification
要选择那种语言为单片机编写程序呢
How does the memory database give full play to the advantages of memory?
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
Flink learning notes (IX) status programming
Successful graduation [2] - student health management system function development...
Design and development of biological instruments
MySQL environment variable configuration
How does the nr-prach receiver detect the relationship between prembleid and Ta
MySQL 数据库基础知识(系统化一篇入门)
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
byte alignment
Uniapp realizes global sharing of wechat applet and custom sharing button style
【男保姆式】教你打开第一个微信小程序
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~
Education is a pass and ticket. With it, you can step into a higher-level environment