JAVA 接口中静态方法

package com.interfaces;

public interface Inter {
    void show();
    default void method(){
        System.out.println("默认方法");
    }
    public static void test(){
        System.out.println("静态方法");
    }
}
package com.interfaces;

public class InterImp implements Inter{
    @Override
    public void show() {
        System.out.println("我是show");
    }

    @Override
    public void method() {
        Inter.super.method();
    }

}
package com.interfaces;

public class Demo {
    public static void main(String[] args) {
        Inter in = new InterImp();
        in.show();
        in.method();
        Inter.test();
    }

}

接口中静态方法只能通过接口名称调用,不能通过实现类名或对象名调用(因为如果在多实现的情况下 ,它不知道调用的是那个接口中的静态方法)

public 可以省略,static不能省略

 

posted @ 2022-04-23 11:51  phpwyl  阅读(973)  评论(0编辑  收藏  举报