第二本 第六章 接口 采用面向接口编程组装一台计算机
//Cpu接口
public interface Cpu {
//获取Cpu的品牌
public String getPp();
//获取Cpu的主频
public String getZp();
}
//Ems 接口
public interface Ems {
//获取容量
public String getRl1();
}
//硬盘接口
public interface HardDisk {
//获取硬盘用量
public String getRl();
}
//个组件厂商, 分别实现 Cpu Ems 硬盘接口
public class Zjcs implements Cpu,Ems,HardDisk{
@Override
public String getRl() {
// TODO Auto-generated method stub
return "30000GB";
}
@Override
public String getPp() {
// TODO Auto-generated method stub
return "Intel";
}
@Override
public String getZp() {
// TODO Auto-generated method stub
return "3.8GHz";
}
@Override
public String getRl1() {
// TODO Auto-generated method stub
return "4GB";
}
//计算机类,组建计算机显示相关信息
public class Jsj {
Cpu cpu;
Ems ems;
HardDisk harddisk;
public void setCpu(Cpu cpu) {
this.cpu = cpu;
}
public void setEms(Ems ems){
this.ems=ems;
}
public void steHardDisk(HardDisk harddisk){
this.harddisk=harddisk;
}
public void print(){
System.out.println("Cpu的品牌是:"+cpu.getPp()+",主频是:"+cpu.getZp());
System.out.println("硬盘容量是"+harddisk.getRl());
System.out.println("内存容量是"+ems.getRl1());
}
//测试组装信息
public static void main(String[] args) {
System.out.println("计算机的信息如下:");
Jsj j=new Jsj();
j.setCpu(new Zjcs());
j.setEms(new Zjcs());
j.steHardDisk(new Zjcs());
j.print();
}