1 package inter; 2 3 public abstract class Door { 4 abstract void openDoor(); 5 abstract void closeDoor(); 6 } 7 8 9 package inter; 10 11 public interface Lock { 12 void openLock(); 13 void closeLock(); 14 } 15 16 package inter; 17 18 public class TheftProofDoor extends Door implements Lock { 19 20 @Override 21 void openDoor() { 22 System.out.println("開門..."); 23 } 24 25 @Override 26 void closeDoor() { 27 System.out.println("關門..."); 28 } 29 30 @Override 31 public void openLock() { 32 System.out.println("開鎖..."); 33 } 34 35 @Override 36 public void closeLock() { 37 System.out.println("關鎖..."); 38 } 39 40 } 41 42 43 package inter; 44 45 public class Test { 46 47 public static void main(String[] args) { 48 TheftProofDoor door=new TheftProofDoor(); 49 // //回家:開鎖 開門 關門 關鎖 50 door.openLock(); 51 door.openDoor(); 52 door.closeDoor(); 53 door.closeLock(); 54 } 55 56 } 57 58 package inter_1; 59 60 public interface InkBox { 61 void printColor();//約定:能夠打印任意顏色 62 } 63 64 65 66 package inter_1; 67 68 public interface Paper { 69 void printSize();//約定:打印時必須知道紙張大小 70 } 71 72 73 package inter_1; 74 75 public class PrinterA implements InkBox,Paper{ 76 77 @Override 78 public void printSize() { 79 System.out.println("A4紙張"); 80 } 81 82 @Override 83 public void printColor() { 84 System.out.println("彩色"); 85 } 86 87 88 } 89 90 91 package inter_1; 92 93 94 public class PrinterB implements InkBox,Paper { 95 @Override 96 public void printColor() { 97 System.out.println("黑白"); 98 } 99 100 @Override 101 public void printSize() { 102 System.out.println("B5紙張"); 103 } 104 } 105 106 package inter_1; 107 108 public class Test { 109 110 public static void main(String[] args) { 111 PrinterA a=new PrinterA(); 112 a.printColor(); 113 a.printSize(); 114 115 PrinterB b=new PrinterB(); 116 b.printColor(); 117 b.printSize(); 118 } 119 120 }