java接口定义的静态方法和默认如何在类实现的时候使用
2024-08-27 10:52:58
在 JDK1.8,允许我们给接口添加两种非抽象的方法实现:
1、默认方法,添加 default 修饰即可;
2、静态方法,使用 static 修饰;示例如下:
这样可以实现接口的增强,那我们在类实现接口的时候如何用呢,我们举例来说明:
首先,我有一个Inteface的接口,如下:Inteface.java:
package Inteface; public interface Inteface {
default String get(String aa){
System.out.print(aa);
return "";
}
static void state(){
System.out.print("state");
};
}
这里面我写了一个默认方法和静态方法,以实现接口的加强,那么,看一下我们的来实现接口是怎么写的吧:
IntefaceS.java:
package Inteface; public class IntefaceS implements Inteface {
public static void move() {
System.out.print("hahah");
} public static void main(String args[]) {
IntefaceS inte = new IntefaceS();
inte.get("123");
Inteface.state();
}
}
由于在接口中使用了static关键字,所以这里再勒种使用的时候需要使用接口去掉方法,这里和类的继承非常相似了;
好吧,看结果
最新文章
- 实现如下类之间的继承关系,并编写Music类来测试这些类。
- linux tricks 之VA系列函数.
- ByteBuffer
- java内部类的继承
- Unity3D的LightProbe动态光探头用法介绍
- vm内核参数优化设置
- IOS,发短信,发邮件,打电话
- [翻译]ASP.NET Web API的路由
- HttpClient 发送图片
- css实现两端对齐的3种方法
- javascript 简易文本编辑器
- 高性能平滑动画_requestAnimationFrame
- linux和shell的学习记录
- Mysql出现(10061)错误提示的暴力解决办法
- 指导手册05:MapReduce编程入门
- Python scrapy - Login Authenication Issue
- BIOS 搭配 MBR/GPT 的开机流程
- transform.forward和vector3.forward
- uc浏览器app点评
- c递归程序
热门文章
- python 调用pymysql模块遇到\转义问题解决办法
- python新模块:secrets模块(为账户密码)
- mysql 查看某个库下面某个表的所有列字段, columnName
- 【转】【MySQL】Mysql模糊查询like提速优化
- SpringBoot系列九:SpringBoot服务整合(整合邮件服务、定时调度、Actuator监控)
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
- e612. Moving the Focus to the Next or Previous Focusable Component
- 升级win10,提示(RAM)内存不足2G的解决的方法,亲測可行
- mothur trim.seqs 去除PCR引物
- List&;Map&;Set的操作和遍历