验证接口实现类对于接口中所有方法是否都要重写?

 1 package Verify;
 2 
 3 interface Output{
 4     //抽象方法
 5     void out();
 6     void get();
 7 
 8     //默认方法,使用default关键字
 9     default void print(){
10         System.out.println("I Love You!接口里的默认方法。");
11     }
12 
13     //类方法,使用static关键字
14     static String staticTest(){
15         return "I Love You!接口里的类方法。";
16     }
17 }
18 public class InterfaceTest implements Output{
19     @Override
20     public void out() {
21         System.out.println("必须重写接口里的抽象方法1");
22     }
23 
24     @Override
25     public void get() {
26         System.out.println("必须重写接口里的抽象方法2");
27     }
28 
29     public static void main(String[] args){
30         InterfaceTest test = new InterfaceTest();
31 
32         test.out();
33         test.get();//结论:接口的抽象方法在实现类中属于必须被重写的
34         test.print();//结论:接口的默认方法在实现类中属于可重写也可不重写
35 //        test.staticTest();//结论:Static method may be invoked on containing interface class only
36         System.out.println(Output.staticTest());//结论:接口的静态方法也叫类方法,则是使用接口来调用
37     }
38 }

结果截图:

猜你喜欢

转载自www.cnblogs.com/masker99/p/12578309.html