【接口】
1 package com.computer; 2 /** 3 *- CPU接口 4 *- 需要厂商返回 品牌和主频 5 *2019-07-19 6 * @author L 7 * 8 */ 9 public interface CPU { 10 public String getBrand();//品牌 11 public String getMain();//主频 12 }
1 package com.computer; 2 /** 3 * - CPU厂商 4 * 2019-07-19 5 * @author L 6 * 7 */ 8 public class CPUVendor implements CPU{ 9 10 @Override 11 public String getBrand() { 12 // TODO Auto-generated method stub 13 return "Inter"; 14 } 15 16 @Override 17 public String getMain() { 18 // TODO Auto-generated method stub 19 return "2.00 GHz"; 20 } 21 22 23 24 }
1 package com.computer; 2 /** 3 * - EMS内存接口 4 * - 需要厂商返回 类型和容量 5 * 2019-07-19 6 * @author L 7 * 8 */ 9 public interface EMS { 10 public String getType();//内存类型 11 public String getCapacity();//内存容量 12 13 }
1 package com.computer; 2 /** 3 * - 内存厂商 4 * 2019-07-19 5 * @author L 6 * 7 */ 8 public class EMSVendor implements EMS{ 9 10 11 @Override 12 public String getType() { 13 // TODO Auto-generated method stub 14 return "DDR SDRAM"; 15 } 16 17 @Override 18 public String getCapacity() { 19 // TODO Auto-generated method stub 20 return "4GB"; 21 } 22 23 }
1 package com.computer; 2 /** 3 * - 硬盘接口HardDisk 4 * - 需要厂商返回 容量 5 * @author L 6 * 7 */ 8 public interface HardDisk { 9 public String getCapacity();//硬盘容量 10 }
1 package com.computer; 2 /** 3 * - 硬盘厂商 4 * 2019-07-19 5 * @author L 6 * 7 */ 8 public class HardDiskVendor implements HardDisk{ 9 10 @Override 11 public String getCapacity() { 12 // TODO Auto-generated method stub 13 return "3000GB"; 14 } 15 16 }
【电脑】
扫描二维码关注公众号,回复:
6832089 查看本文章
1 package com.computer; 2 /** 3 *- 组装一台计算机 4 *- 需要三个接口,CPU接口,内存接口EMS,硬盘接口HardDisk 5 *2019-07-19 6 * @author L 7 * 8 */ 9 public class Computer { 10 private CPU cpu; 11 private EMS ems; 12 private HardDisk harddisk; 13 14 public CPU getCpu() { 15 return cpu; 16 } 17 public void setCpu(CPU cpu) { 18 this.cpu = cpu; 19 } 20 public EMS getEms() { 21 return ems; 22 } 23 public void setEms(EMS ems) { 24 this.ems = ems; 25 } 26 public HardDisk getHardisk() { 27 return harddisk; 28 } 29 public void setHardisk(HardDisk harddisk) { 30 this.harddisk = harddisk; 31 } 32 33 34 //计算机信息输出 35 public void print() { 36 System.out.println("计算机的信息如下:"); 37 System.out.println("CPU的品牌是:"+cpu.getBrand()+",主频是:"+cpu.getMain()); 38 System.out.println("硬盘的容量是:"+harddisk.getCapacity()); 39 System.out.println("内存的容量是:"+ems.getCapacity()+",类型是:"+ems.getType()); 40 } 41 42 }
【测试】
1 package com.computer; 2 /** 3 * - 测试类 4 * - 输出计算机的信息 5 * 2019-07-19 6 * @author L 7 * 8 */ 9 public class Test { 10 public static void main(String[] args) { 11 Computer computer=new Computer(); 12 CPU cpu =new CPUVendor(); 13 EMS ems =new EMSVendor(); 14 HardDisk harddisk=new HardDiskVendor(); 15 16 computer.setCpu(cpu); 17 computer.setEms(ems); 18 computer.setHardisk(harddisk); 19 20 computer.print(); 21 22 23 24 } 25 }
【结果】