电脑
电脑类
package cn.computer; public class Computer { private CPU cpu; private EMS ems; private HardDisk hardDisk; public void print() { System.out.println("CPU的品牌是:"+cpu.pingpai()+",主频是:"+cpu.zhuping()+"Ghz"); System.out.println("硬盘 的类型是:"+ems.type()+"硬盘容量是:"+ems.capacity()+"GB"); System.out.println("内存容是:"+hardDisk.capacity()+"GB"); } public CPU getCpu() { return cpu; } public void setCpu(CPU cpu) { this.cpu = cpu; } public EMS getEms() { return ems; } public void setEms(EMS ems) { this.ems = ems; } public HardDisk getHardDisk() { return hardDisk; } public void setHardDisk(HardDisk hardDisk) { this.hardDisk = hardDisk; } }
接口类
package cn.computer; //接口 public interface CPU { public String pingpai(); public double zhuping(); } package cn.computer; //接口 public interface EMS { public String type(); public int capacity(); } package cn.computer; //接口 public interface HardDisk { public int capacity(); }
接口实现类
package cn.computer; public class C1 implements CPU{ public String pingpai() { return "Intel"; } public double zhuping() { //double z = 3.8; return 3.8; } } package cn.computer; public class E2 implements EMS{ public String type() { return "富士康"; } public int capacity() { return 3000; } } package cn.computer; public class H3 implements HardDisk{ public int capacity() { return 4; } }
测试类
package cn.computer; public class Test { public static void main(String[] args) { Computer computer = new Computer(); System.out.println("计算机的信息如下:"); CPU cpu = new C1(); EMS ems = new E2(); HardDisk hardDisk = new H3(); computer.setCpu(cpu);; computer.setEms(ems); computer.setHardDisk(hardDisk); computer.print(); } }