CPU:
package com.cpu; public interface Cpu { public String xinHao(); public String zhuPin(); }
CPU型号和主频:
package com.cpu; public class CpuBrand implements Cpu{ @Override public String xinHao() { // TODO Auto-generated method stub return "Intel"; } @Override public String zhuPin() { // TODO Auto-generated method stub return "3.8GHz"; } }
ems:
package com.EMS; public interface Ems { public String EMS(); }
ems容量:
package com.EMS; public class EmsCapacity implements Ems { @Override public String EMS() { // TODO Auto-generated method stub return "3000GB"; } }
硬盘:
package com.HardDisk; public interface HardDisk { public String hardDisk(); }
硬盘容量:
package com.HardDisk; public class HardDiskCapacity implements HardDisk { @Override public String hardDisk() { // TODO Auto-generated method stub return "4GB"; } }
电脑类:
package com.master; import com.EMS.Ems; import com.HardDisk.HardDisk; import com.cpu.Cpu; /** * 电脑类 */ public class Computer { private Cpu cpu; private Ems ems; private HardDisk hardDisk; 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; } public void print() { System.out.println("计算机的信息如下:"); System.out.println("CPU的品牌是:"+cpu.xinHao()+",主频是"+cpu.zhuPin()); System.out.println("硬盘容量是:"+ems.EMS()); System.out.println("内存容量是:"+hardDisk.hardDisk()); } }
测试类:
package com.master; import com.EMS.Ems; import com.EMS.EmsCapacity; import com.HardDisk.HardDisk; import com.HardDisk.HardDiskCapacity; import com.cpu.Cpu; import com.cpu.CpuBrand; public class TestComputer { public static void main(String[] args) { Cpu cpu = new CpuBrand(); Ems ems = new EmsCapacity(); HardDisk hardDisk = new HardDiskCapacity(); Computer computer = new Computer(); computer.setCpu(cpu); computer.setEms(ems); computer.setHardDisk(hardDisk); computer.print(); } }
输出:
原文地址:https://www.cnblogs.com/Zhangchuanfeng1/p/10398533.html
时间: 2024-10-09 18:35:56