当前位置:网站首页>(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两者的区别介绍
- 默认方法通过实例调用,静态方法通过接口名调用
- 默认方法可以被继承,实现类可以直接调用接口默认方法,也可以重写接口默认方法
- 静态方法不能被继承,实现类不能重写接口的静态方法,只能使用接口名调用
边栏推荐
- STM32 serial port usart1 routine
- 我想各位朋友都应该知道学习的基本规律就是:从易到难
- Stm32f04 clock configuration
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 3 --blinker_ MIOT_ Light (lighting technology app control + Xiaoai classmate control)
- The third paper of information system project manager in soft examination
- 学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
- 万字手撕七大排序(代码+动图演示)
- [combinatorics] Introduction to Combinatorics (combinatorial thought 2: mathematical induction | mathematical induction promotion | multiple induction thought)
- Successful graduation [3]- blog system update...
- Getting started with JMX, MBean, mxbean, mbeanserver
猜你喜欢
![[CSDN] C1 training problem analysis_ Part II_ Web Foundation](/img/91/72cdea3eb3f61315595330d2c9016d.png)
[CSDN] C1 training problem analysis_ Part II_ Web Foundation

NR technology -- MIMO

单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了

Design and development of biological instruments

Eight working modes of stm32gpio and chip naming rules

Idea remote breakpoint debugging jar package project
![[combinatorics] Introduction to Combinatorics (combinatorial thought 2: mathematical induction | mathematical induction promotion | multiple induction thought)](/img/0e/dff6d3c6b7bf388bf71b0965533c25.jpg)
[combinatorics] Introduction to Combinatorics (combinatorial thought 2: mathematical induction | mathematical induction promotion | multiple induction thought)

Flink learning notes (VIII) multi stream conversion

Quelle langue choisir pour programmer un micro - ordinateur à puce unique

【男保姆式】教你打开第一个微信小程序
随机推荐
Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
Epollet lessons
端午节快乐!—— canvas写的粽子~~~~~
Convert IP address to int
STM32 interrupt priority management
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
嵌入式本来就很坑,相对于互联网来说那个坑多得简直是难走
How does the memory database give full play to the advantages of memory?
Uniapp realizes global sharing of wechat applet and custom sharing button style
Stm32f407 key interrupt
2021-01-03
Vector processor 9_ Basic multilevel interconnection network
Error output redirection
2020-08-23
Flink learning notes (XI) table API and SQL
Install local sources using yum
內存數據庫究竟是如何發揮內存優勢的?
In third tier cities and counties, it is difficult to get 10K after graduation
MySQL data manipulation language DML common commands
Nr-prach: access scenario and access process