用法:
1 1 interface A { 2 2 public static final int i = 10; 3 3 4 4 public void runLoad(); 5 5 } 6 6 7 7 public class Demo implements A { 8 8 // 实现接口中的方法 9 9 @Override 1010 public void runLoad() { 1111 System.out.println("这是调用的接口中的方法"); 1212 } 1313 1414 public static void main(String[] args) { 1515 Demo d = new Demo(); 1616 d.runLoad(); 1717 } 1818 }
示例:
1 1 //铅笔 2 2 class Pencil { 3 3 String name; 4 4 5 5 public Pencil(String name) { 6 6 this.name = name; 7 7 } 8 8 9 9 public void writer() { 1010 System.out.println(name + "写字"); 1111 } 1212 } 1313 1414 //橡皮檫接口 1515 interface Eraser { 1616 public void remove(); 1717 } 1818 1919 //带橡皮檫的铅笔 2020 class PencilEraser extends Pencil implements Eraser { 2121 public PencilEraser(String name) { 2222 super(name); 2323 } 2424 2525 @Override 2626 public void remove() { 2727 System.out.println(name + "涂改"); 2828 } 2929 } 3030 3131 public class DemoPencil { 3232 public static void main(String[] args) { 3333 PencilEraser p = new PencilEraser("2B铅笔"); 3434 p.writer(); 3535 p.remove(); 3636 } 3737 }