当前位置:网站首页>(2) New methods in the interface
(2) New methods in the interface
2022-07-03 09:58:00 【look-word】
Methods added to the interface
1. JDK8 New interface in
stay JDK8 The interface is enhanced in , stay JDK8 Before
interface The interface name {
static const ;
Abstract method ;
}
JDK8 After that, the interface was added , There can be The default method and Static methods
interface The interface name {
static const ;
Abstract method ;
The default method ;
Static methods ;
}
2 The default method
2.1 Why add the default method
stay JDK8 Previously, there were only abstract methods and static constants in interfaces , There will be the following problems :
If an abstract method is added to the interface , Then the implementation class must abstract this abstract method , It's not conducive to interface expansionAdd abstract method in interface , All implementation classes need to override this method , Not conducive to interface expansion
2.2 Interface default method format
The syntax format of the default method in the interface is
interface The interface name {
Modifier default return type Method name {
Method body ;
}
}
2.3 The use of default methods in the interface
There are two ways to use the default methods in an interface
- The implementation class calls the default method of the interface directly
- The default method that implements the class override interface
3. Static methods
JDK8 Static methods are added to the interface in , The function is also to expand the interface
3.1 Rule of grammar
interface The interface name {
Modifier static return type Method name {
Method body ;
}
}
3.2 Use of static methods
Static methods in an interface cannot be overridden in an implementation class , It can only be called through the interface type : The interface name . Static method name ();
If you want to trust your friends, you will have such questions Can the static methods in the interface be inherited Class implementation interface Can you call static methods in the interface
4 The difference between the two is introduced
- The default method is called by instance , Static methods are called by interface names
- Default methods can be inherited , The implementation class can directly call the interface default method , You can also override interface default methods
- Static methods cannot be inherited , An implementation class cannot override a static method of an interface , You can only call... With an interface name
边栏推荐
- I think all friends should know that the basic law of learning is: from easy to difficult
- el-table X轴方向(横向)滚动条默认滑到右边
- STM32 running lantern experiment - library function version
- I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
- 对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
- 2021-10-28
- 单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
- The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
- MySQL的简单使用(增删改查)
- JS基础-原型原型链和宏任务/微任务/事件机制
猜你喜欢
uniapp 实现微信小程序全局分享及自定义分享按钮样式
Which language should I choose to program for single chip microcomputer
An executable binary file contains more than machine instructions
JS基础-原型原型链和宏任务/微任务/事件机制
Windows下MySQL的安装和删除
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
There is no specific definition of embedded system
万字手撕七大排序(代码+动图演示)
Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
Pymssql controls SQL for Chinese queries
随机推荐
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
內存數據庫究竟是如何發揮內存優勢的?
万字手撕七大排序(代码+动图演示)
Matlab reads hexadecimal numbers and converts them into signed short
In third tier cities and counties, it is difficult to get 10K after graduation
【順利畢業】[1]-遊覽 [學生管理信息系統]
Gpiof6, 7, 8 configuration
Intelligent home design and development
A lottery like scissors, stone and cloth (C language)
Electronic product design, MCU development, circuit cloning
Nr--- Pusch I: sorting out the agreement process
Notes on C language learning of migrant workers majoring in electronic information engineering
[successful graduation] [1] - visit [student management information system]
Windows下MySQL的安装和删除
Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
There is no specific definition of embedded system
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
要選擇那種語言為單片機編寫程序呢
JMX、MBean、MXBean、MBeanServer 入门