1. 背景介绍
在Java8之前,接口中定义的方法都是抽象方法,即默认都是被public abstract
修饰的;但从Java8开始,允许在接口中定义带有方法体的默认方法和静态方法;
| public class InterfaceTest { |
| |
| public static void main(String[] args) { |
| |
| } |
| } |
| |
| interface InterfaceA { |
| |
| |
| |
| |
| default void defaultMethod() { |
| System.out.println("InterfaceA defaultMethod"); |
| } |
| } |
| |
| interface InterfaceB { |
| |
| |
| |
| |
| static void staticMethod() { |
| System.out.println("InterfaceB staticMethod"); |
| } |
| } |
| |
2. 接口的默认方法介绍
- 使用
default
关键字修饰,实现类对父类接口的默认方法可选择性重写,且接口的默认方法只能通过实现类对象调用;
| public class InterfaceTest implements InterfaceA { |
| |
| public static void main(String[] args) { |
| InterfaceA a1 = new InterfaceTest(); |
| |
| a1.defaultMethod(); |
| InterfaceA a2 = new InterfaceAImpl(); |
| a2.defaultMethod(); |
| } |
| |
| |
| |
| |
| @Override |
| public void defaultMethod() { |
| System.out.println("InterfaceTest defaultMethod"); |
| } |
| } |
| |
| |
| |
| |
| class InterfaceAImpl implements InterfaceA { |
| |
| } |
| |
| interface InterfaceA { |
| |
| |
| |
| |
| default void defaultMethod() { |
| System.out.println("InterfaceA defaultMethod"); |
| } |
| } |
| |
| // 运行结果 |
| InterfaceTest defaultMethod |
| InterfaceA defaultMethod |
| |
当实现类拥有多个父类接口,且多个父类接口含有相同的默认方法时,实现类则必须重写该默认方法,否则会出现编译错误;
| public class InterfaceTest implements InterfaceA, InterfaceB { |
| |
| public static void main(String[] args) { |
| InterfaceA a = new InterfaceTest(); |
| a.defaultMethod(); |
| InterfaceB b = new InterfaceTest(); |
| b.defaultMethod(); |
| InterfaceTest test = new InterfaceTest(); |
| test.defaultMethod(); |
| } |
| |
| |
| |
| |
| @Override |
| public void defaultMethod() { |
| System.out.println("InterfaceTest defaultMethod"); |
| } |
| } |
| |
| interface InterfaceA { |
| |
| default void defaultMethod() { |
| System.out.println("InterfaceA defaultMethod"); |
| } |
| } |
| |
| interface InterfaceB { |
| |
| default void defaultMethod() { |
| System.out.println("InterfaceB defaultMethod"); |
| } |
| } |
| |
| // 运行结果 |
| InterfaceTest defaultMethod |
| InterfaceTest defaultMethod |
| InterfaceTest defaultMethod |
| |
实现类对于多层父类接口的默认方法采用就近原则继承;
| public class InterfaceTest implements InterfaceA, InterfaceSuper { |
| |
| public static void main(String[] args) { |
| |
| InterfaceSuper i = new InterfaceTest(); |
| i.defaultMethod(); |
| } |
| } |
| |
| interface InterfaceA extends InterfaceSuper { |
| |
| default void defaultMethod() { |
| System.out.println("InterfaceA defaultMethod"); |
| } |
| } |
| |
| interface InterfaceSuper { |
| |
| default void defaultMethod() { |
| System.out.println("InterfaceSuper defaultMethod"); |
| } |
| } |
| |
| // 运行结果 |
| InterfaceA defaultMethod |
| |
3. 接口的静态方法介绍
使用static
关键字修饰,访问控制修饰符默认是public
的,且静态方法的调用只能通过接口类;
| public class InterfaceTest { |
| |
| public static void main(String[] args) { |
| |
| InterfaceB.staticMethod(); |
| } |
| } |
| |
| |
| |
| |
| interface InterfaceB { |
| |
| static void staticMethod() { |
| System.out.println("InterfaceB staticMethod"); |
| } |
| } |
| |
| // 运行结果 |
| InterfaceB staticMethod |
来源:https://blog.csdn.net/gu19930914/article/details/115718050
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)