JAVA接口中不可以有静态方法吗

1. 接口中每一个方法也是隐式抽象的,接口中的方法会被隐式的指定为 public abstract(只能是 public abstract,其他修饰符都会报错),所以不能含有静态代码块以及静态方法(用 static 修饰的方法)

2. 在jdk1.8中,接口里可以有静态方法,接口里的有静态方法 必须要有body。有静态方法不需要实现。

public interface testInter {
    void printme();

    static void print_s(){
        System.out.println("print in static method in interface");
    }

}

class testInterImpl implements testInter{

    public void printme() {
        System.out.println("me");
    }
}

public class TestMain {

    public static void main(String[] args) {
        System.out.println("123");

        testInterImpl t = new testInterImpl();
        t.printme();
        testInter.print_s();
    }
}

猜你喜欢

转载自www.cnblogs.com/baxianhua/p/9330172.html