计算机硬件接口类:
/**
* @author Administrator
* CPU接口
*/
public interface CPU {
public String getBand();
public String getZhuPin();
}
/**
* @author Administrator
* 内存接口
*/
public interface Dimm {
public String dimmSize();
}
/**
* @author Administrator
* 硬盘接口
*/
public interface SD {
public String sdSize();
}
硬件生产商类:
/**
* @author Administrator
* CPU生产商
*/
public class CPUpro implements CPU{
public String getBand() {
return "Intel";
}
public String getZhuPin() {
return "3.8GHz";
}
}
/**
* @author Administrator
* 内存生产商
*/
public class DimmPro implements Dimm{
@Override
public String dimmSize() {
// TODO Auto-generated method stub
return "4GB";
}
}
/**
* @author Administrator
* 硬盘生产商
*/
public class SDpro implements SD{
@Override
public String sdSize() {
return "3000GB";
}
}
计算机类:
/**
* @author Administrator
* 计算机类
*/
public class Computer {
private CPU cpu;
private SD sd;
private Dimm dimm;
public Computer() {}
public Computer(CPU cpu, SD sd, Dimm dimm) {
super();
this.cpu = cpu;
this.sd = sd;
this.dimm = dimm;
}
public void print() {
System.out.println("计算机的信息如下:");
System.out.println("CPU的品牌是:"+cpu.getBand()+",主频是:"+cpu.getZhuPin());
System.out.println("硬盘容量是:"+sd.sdSize());
System.out.println("内存容量是:"+dimm.dimmSize());
}
public CPU getCpu() {
return cpu;
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
public SD getSd() {
return sd;
}
public void setSd(SD sd) {
this.sd = sd;
}
public Dimm getDimm() {
return dimm;
}
public void setDimm(Dimm dimm) {
this.dimm = dimm;
}
}
测试类:
public class Text {
public static void main(String[] args) {
CPU cpu = new CPUpro();
SD sd = new SDpro();
Dimm dimm = new DimmPro();
Computer computer = new Computer(cpu,sd,dimm);
computer.print();
}
}
运行结果: