1 package 面向对象; 2 3 public interface ICPU { 4 5 //定义CPU接口 6 public void PinPai1(); 7 public void Name1(); 8 public void XingHao1(); 9 10 } 11 12 13 14 public interface IScrean { 15 16 //定义显示器接口 17 public void PinPai2(); 18 public void XingHao2(); 19 public void Size2(); 20 } 21 22 23 24 public class SHIXIAN implements ICPU, IScrean { 25 //实现接口 26 @Override 27 public void PinPai2() { 28 System.out.println("中华映管"); 29 30 } 31 32 @Override 33 public void XingHao2() { 34 System.out.println("CPT17AB"); 35 36 } 37 38 @Override 39 public void Size2() { 40 System.out.println("14英寸"); 41 42 } 43 44 @Override 45 public void PinPai1() { 46 System.out.println("Intel"); 47 48 } 49 50 @Override 51 public void Name1() { 52 System.out.println("酷睿"); 53 54 } 55 56 @Override 57 public void XingHao1() { 58 System.out.println("LGA2011"); 59 60 } 61 62 } 63 64 65 66 //创建电脑类 67 public class Computer { 68 69 //定义成员变量 70 private ICPU cpu; 71 private IScrean screan; 72 //生成get、set方法 73 public ICPU getCpu() { 74 return cpu; 75 } 76 public void setCpu(ICPU cpu) { 77 this.cpu = cpu; 78 } 79 public IScrean getScrean() { 80 return screan; 81 } 82 public void setScrean(IScrean screan) { 83 this.screan = screan; 84 } 85 //成员方法 86 public boolean zuzhuang() 87 { 88 boolean b = false; 89 if(cpu==null) 90 { 91 System.out.println("没有CPU,电脑未组装成功。"); 92 b = false; 93 } 94 else if(screan == null) 95 { 96 System.out.println("没有显示器。电脑未组装成功。"); 97 b = false; 98 } 99 else 100 { 101 System.out.println("有CPU,有显示器。电脑组装成功。"); 102 b = true; 103 } 104 return b; 105 106 } 107 } 108 109 110 111 //测试 112 public class testcar { 113 114 public static void main(String[] args) { 115 116 Computer computer = new Computer(); 117 computer.setCpu(new SHIXIAN()); 118 computer.setScrean(new SHIXIAN()); 119 computer.zuzhuang(); 120 } 121 }