场景:
    在接口中,如果定义了多个默认方法,默认方法又需要封装可以重复使用的方法来执行重复的代码,但是如果使用默认方法来作为公共方法在其他默认方法中使用,这样在实现类中会发生重复,所以,在接口中定义公共方法需要定义私有方法,私有方法就用于当前的接口中的默认方法里面
    接口:

    1. package com.interfPack;
    2. public interface MyInterfaceA {
    3. public default void defaultMethd1(){
    4. System.out.println("默认1");
    5. }
    6. public default void defaultMethd2(){
    7. System.out.println("默认2");
    8. defaultCommon()//错误写法
    9. }
    10. public default void defaultCommon(){ //错误写法
    11. System.out.println("默认公共");
    12. }
    13. }

    实现类:

    1. package com.interfPack;
    2. public class InterfaceADemo implements MyInterfaceA{
    3. }

    调用:

    1. package com.interfPack;
    2. public class UseDemo {
    3. public static void main(String[] args) {
    4. InterfaceADemo interfaceADemo=new InterfaceADemo();
    5. interfaceADemo.defaultMethd1();
    6. interfaceADemo.defaultMethd2();
    7. }
    8. }

    image.png