当前位置:网站首页>(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两者的区别介绍
- 默认方法通过实例调用,静态方法通过接口名调用
- 默认方法可以被继承,实现类可以直接调用接口默认方法,也可以重写接口默认方法
- 静态方法不能被继承,实现类不能重写接口的静态方法,只能使用接口名调用
边栏推荐
- Schematic diagram and connection method of six pin self-locking switch
- Project cost management__ Plan value_ Earned value_ Relationship among actual cost and Countermeasures
- 2020-08-23
- Nr--- Pusch I: sorting out the agreement process
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- UCI and data multiplexing are transmitted on Pusch - Part I
- 万字手撕七大排序(代码+动图演示)
- Development of electrical fire system
- Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
- Stm32-hal library learning, using cubemx to generate program framework
猜你喜欢
![【顺利毕业】[1]-游览 [学生管理信息系统]](/img/91/72cdea3eb3f61315595330d2c9016d.png)
【顺利毕业】[1]-游览 [学生管理信息系统]

When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn

Exception handling of arm

An executable binary file contains more than machine instructions

Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode

编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解

对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门

Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
![顺利毕业[3]-博客系统 更新中。。。](/img/91/72cdea3eb3f61315595330d2c9016d.png)
顺利毕业[3]-博客系统 更新中。。。

Flink learning notes (IX) status programming
随机推荐
[successful graduation] [1] - visit [student management information system]
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)
Project scope management__ Scope management plan and scope specification
All processes of top ten management in project management
内存数据库究竟是如何发挥内存优势的?
Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
STM32 interrupt priority management
MySQL environment variable configuration
Flink learning notes (XI) table API and SQL
要选择那种语言为单片机编写程序呢
UCI and data multiplexing are transmitted on Pusch - determine the bit number of harqack, csi1 and csi2 (Part II)
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
Error output redirection
Qt QComboBox QSS样式设置
Convert IP address to int
Eight working modes of stm32gpio and chip naming rules
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
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
MySQL Data Definition Language DDL common commands
Exception handling of arm