1 package day13.lesson2;
2
3 /*
4 2.2 接口中静态方法
5 格式
6 [public] static 返回值类型 方法名(参数列表) { }
7 范例
8 [public] static void show() {
9 }
10 注意事项
11 静态方法只能通过接口名调用,不能通过实现类名或者对象名调用
12 public可以省略,static不能省略
13 */
14 public class InterDemo {
15 public static void main(String[] args) {
16 Inter i = new InterImpl();
17 i.show();
18 i.method();
19
20 // i.test(); //编译异常
21 // InterImpl.test; //编译异常
22 //歧义:当InterImpl实现两个接口时,编译器不知道对象i要调用Inter中的test还是Flyable中的
23
24 Inter.test(); //ok
25 Flyable.test(); //ok
26 }
27 }
28
29 interface Inter{
30 //抽象方法
31 void show();
32
33 //默认方法
34 default void method(){
35 System.out.println("接口Inter中的默认方法method");
36 }
37
38 //静态方法
39 /*public static void test(){
40 System.out.println("接口Inter中的静态方法test");
41 }*/
42 static void test(){
43 System.out.println("接口Inter中的静态方法test");
44 }
45 }
46
47 class InterImpl implements Inter, Flyable{
48 @Override
49 public void show() {
50 System.out.println("实现类InterImpl重写接口Inter中的抽象方法show");
51 }
52 }
53
54 interface Flyable{
55 static void test(){
56 System.out.println("接口Flyable中的静态方法test");
57 }
58 }
