JAVA的接口

用法:

 1 interface A {
 2     public static final int i = 10;
 3 
 4     public void runLoad();
 5 }
 6 
 7 public class Demo implements A {
 8     // 实现接口中的方法
 9     @Override
10     public void runLoad() {
11         System.out.println("这是调用的接口中的方法");
12     }
13 
14     public static void main(String[] args) {
15         Demo d = new Demo();
16         d.runLoad();
17     }
18 }

示例:

 1 //铅笔
 2 class Pencil {
 3     String name;
 4 
 5     public Pencil(String name) {
 6         this.name = name;
 7     }
 8 
 9     public void writer() {
10         System.out.println(name + "写字");
11     }
12 }
13 
14 //橡皮檫接口
15 interface Eraser {
16     public void remove();
17 }
18 
19 //带橡皮檫的铅笔
20 class PencilEraser extends Pencil implements Eraser {
21     public PencilEraser(String name) {
22         super(name);
23     }
24 
25     @Override
26     public void remove() {
27         System.out.println(name + "涂改");
28     }
29 }
30 
31 public class DemoPencil {
32     public static void main(String[] args) {
33         PencilEraser p = new PencilEraser("2B铅笔");
34         p.writer();
35         p.remove();
36     }
37 }
posted @ 2018-10-17 17:44  ronle  阅读(180)  评论(0编辑  收藏  举报