天天看点

java接口

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 }      

有的要别人来设定目标,有的给别人设定目标;

有的需要感情支持生活,有的需要意志支持生活。

上一篇: Java接口
下一篇: 网络编程