接口的组成
为什么接口中的静态方法不能通过实现类类名调用?
答:假如有两个不同的接口,并且他们的静态方法名都为test(),并且一个实现类同时实现了这两个接口,如果可以通过实现类名调用静态方法,那么 实现类.test()就不知道调用的是这两个接口的其中哪一个静态方法了
代码演示:
public interface inter {
//默认方法
private void show() {
System.out.println("Java初级程序员");
System.out.println("Java中级程序员");
System.out.println("Java高级程序员");
}
default void show1() {
System.out.println("show1方法开始执行了");
// System.out.println("Java初级程序员");
// System.out.println("Java中级程序员");
// System.out.println("Java高级程序员");
show();//默认的可以调用私有的非静态方法
method();//默认的也可以调用私有的静态方法
System.out.println("show1方法执行结束了");
}
default void show2() {
System.out.println("show2方法开始执行了");
// System.out.println("Java初级程序员");
// System.out.println("Java中级程序员");
// System.out.println("Java高级程序员");
show();//默认的可以调用私有的非静态方法
method();//默认的也可以调用私有的静态方法
System.out.println("show2方法执行结束了");
}
//私有的静态方法
private static void method(){
System.out.println("Java初级程序员");
System.out.println("Java中级程序员");
System.out.println("Java高级程序员");
}
private static void method1() {
System.out.println("method1方法开始执行了");
// System.out.println("Java初级程序员");
// System.out.println("Java中级程序员");
// System.out.println("Java高级程序员");
method();
// show();//静态的不能调用非静态方法
System.out.println("method1方法执行结束了");
}
private static void method2() {
System.out.println("method2方法开始执行了");
// System.out.println("Java初级程序员");
// System.out.println("Java中级程序员");
// System.out.println("Java高级程序员");
method();
System.out.println("method2方法执行结束了");
}
}