JAVA 接口中静态方法

2022/4/23 12:42:34

本文主要是介绍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不能省略

 



这篇关于JAVA 接口中静态方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程