day10--接口的默认方法的定义和使用

This commit is contained in:
xx
2026-10-08 11:49:22 +08:00
parent 7a9bf4dce7
commit 4086ee1dd5
3 changed files with 54 additions and 0 deletions
@@ -0,0 +1,19 @@
package com.inmind.interface_default02;
/*
JDK8时提供,接口的默认方法
接口的默认方法的使用步骤:
1.接口不能创建对象的,它没有构造方法
2.定义一个实现类,实现该接口,并实现接口中的所有的抽象方法
public class MyInterfaceImpl(实现类) implements MyInterface(接口){
3.创建该接口的实现类的对象,调用接口的默认方法
4.在接口的实现类中,可以根据对应的需求,选择性重写,或不重写接口中的默认方法,也可以沿用功能(接口.super.默认方法名)(重点)
*/
public class Demo02 {
public static void main(String[] args) {
MyInterfaceImpl1 myInterfaceImpl1 = new MyInterfaceImpl1();
myInterfaceImpl1.method();
myInterfaceImpl1.method1();
}
}