当前位置:网站首页>(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两者的区别介绍
- 默认方法通过实例调用,静态方法通过接口名调用
- 默认方法可以被继承,实现类可以直接调用接口默认方法,也可以重写接口默认方法
- 静态方法不能被继承,实现类不能重写接口的静态方法,只能使用接口名调用
边栏推荐
- 手机都算是单片机的一种,只不过它用的硬件不是51的芯片
- 干单片机这一行的时候根本没想过这么多,只想着先挣钱养活自己
- An executable binary file contains more than machine instructions
- uniapp 实现微信小程序全局分享及自定义分享按钮样式
- Simple use of MySQL (addition, deletion, modification and query)
- 顺利毕业[3]-博客系统 更新中。。。
- MySQL environment variable configuration
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 5 --blinker_ MIOT_ MULTI_ Outside (lighting technology app + Xiaoai classmate control socket multiple jacks)
- 学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
- 一个可执行的二进制文件包含的不仅仅是机器指令
猜你喜欢

Flink learning notes (IX) status programming
![[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation](/img/06/7fd985faf8806ceface3864d4b3180.png)
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation

Leetcode daily question (2090. K radius subarray averages)

编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
![Successful graduation [3]- blog system update...](/img/91/72cdea3eb3f61315595330d2c9016d.png)
Successful graduation [3]- blog system update...

uniapp 实现微信小程序全局分享及自定义分享按钮样式

Stm32f407 key interrupt

Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
![[CSDN] C1 training problem analysis_ Part II_ Web Foundation](/img/91/72cdea3eb3f61315595330d2c9016d.png)
[CSDN] C1 training problem analysis_ Part II_ Web Foundation

STM32 port multiplexing and remapping
随机推荐
端午节快乐!—— canvas写的粽子~~~~~
byte alignment
Mysql database underlying foundation column
Project cost management__ Topic of comprehensive calculation
Leetcode daily question (931. minimum falling path sum)
MySQL environment variable configuration
Electronic product design, MCU development, circuit cloning
[graduation successful] [1] - tour [Student Management Information System]
Intelligent home design and development
Project cost management__ Cost management technology__ Article 8 performance review
Nodemcu-esp8266 development board to build Arduino ide development environment
Raspberry pie installation SciPy
Leetcode daily question (2109. adding spaces to a string)
Flink CDC practice (including practical steps and screenshots)
Process communication - semaphore
Nr-prach:prach format and time-frequency domain
[keil5 debugging] warning:enumerated type mixed with other type
SSB Introduction (PbCH and DMRs need to be supplemented)
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
Schematic diagram and connection method of six pin self-locking switch