Java抽象类与接口(2) ------接口

1、接口A

 1 package com.cn.donleo.test.model;
 2 
 3 /**
 4  * @author liangd
 5  * date 2020-10-29 15:17
 6  * code
 7  */
 8 public interface InterfaceA {
 9     /**
10      * 默认方法测试
11      */
12     default void a(){
13         System.out.println("a");
14     }
15 
16     /**
17      * 抽象方法
18      * @return
19      */
20     String aa();
21 
22 
23     static void aaa(){
24         System.out.println("aaa");
25     }
26 }

 

2、接口B

 1 package com.cn.donleo.test.model;
 2 
 3 /**
 4  * @author liangd
 5  * date 2020-10-29 15:17
 6  * code
 7  */
 8 public interface InterfaceB {
 9     /**
10      * 默认方法测试
11      */
12     default void b(){
13         System.out.println("b");
14     }
15 
16     /**
17      * 抽象方法
18      * @return
19      */
20     String bb();
21 
22 
23     static void bbb(){
24         System.out.println("bbb");
25     }
26 }

 

3、接口C

 1 package com.cn.donleo.test.model;
 2 
 3 /**
 4  * @author liangd
 5  * date 2020-10-29 15:18
 6  * code
 7  */
 8 public interface InterfaceC {
 9     /**
10      * 默认方法测试
11      */
12     default void c(){
13         System.out.println("c");
14     }
15 
16     /**
17      * 抽象方法
18      * @return
19      */
20     String cc();
21 
22 
23     static void ccc(){
24         System.out.println("ccc");
25     }
26 }

 

4、测试类一

 1 package com.cn.donleo.test;
 2 
 3 /**
 4  * @author liangd
 5  * date 2020-10-29 15:06
 6  * code 接口测试
 7  */
 8 public interface TestInterface {
 9     /**
10      * 接口中的抽象方法不能实现
11      *
12      * @return
13      */
14     int testInt();
15 
16     /**
17      * 在java8 以后,接口中可以添加使用default或者static修饰的方法
18      * default修饰方法只能在接口中使用
19      * @return
20      */
21     default String test(){
22         return "接口中的默认方法";
23     }
24 
25 
26     static String testString(){
27         return "接口类中的静态方法必须实现";
28     }
29 
30     static void main(String[] args){
31         System.out.println(testString());
32     }
33 }

 

5、测试类二

 1 package com.cn.donleo.test;
 2 
 3 import com.cn.donleo.test.model.InterfaceA;
 4 import com.cn.donleo.test.model.InterfaceB;
 5 import com.cn.donleo.test.model.InterfaceC;
 6 
 7 /**
 8  * @author liangd
 9  * date 2020-10-29 15:19
10  * code 接口实现类测试
11  */
12 public class TestInterfaceImpl implements InterfaceA, InterfaceB, InterfaceC {
13 
14     @Override
15     public void a() {
16         System.out.println("子类a");
17     }
18 
19     @Override
20     public void b() {
21         System.out.println("子类b");
22     }
23 
24     @Override
25     public void c() {
26         System.out.println("子类c");
27     }
28 
29     @Override
30     public String aa() {
31         return "aa";
32     }
33 
34     @Override
35     public String bb() {
36         return "bb";
37     }
38 
39     @Override
40     public String cc() {
41         return "cc";
42     }
43 
44     /**
45      * 1、接口中的default方法在实现类中可以继承也可以不被继承
46      * 2、但抽象方法一定要继承
47      * @param args
48      */
49     public static void main(String[] args) {
50         TestInterfaceImpl temp = new TestInterfaceImpl();
51         temp.a();
52         temp.b();
53         temp.c();
54         System.out.println(temp.aa());
55         System.out.println(temp.bb());
56         System.out.println(temp.cc());
57     }
58 }

 

posted @ 2020-12-02 14:16  donleo123  阅读(86)  评论(0编辑  收藏  举报