package come.hanqi; public interface ICpu { //创建接口 //性能 public void xingneng(); //品牌 public void pinpai(); //质量 public void zhiliang(); }
package come.hanqi; public class CPU implements ICpu { //实现接口 @Override public void xingneng() { System.out.println("性能良好"); } @Override public void pinpai() { System.out.println("品牌为:因特尔"); } @Override public void zhiliang() { System.out.println("质量为优等"); } }
package come.hanqi; public class Computer { private CPU cpu; public void cpu() { if(cpu==null) { System.out.println("没有装cpu"); } else { System.out.println("已装cpu"); cpu. xingneng(); cpu.zhiliang(); cpu.pinpai(); } } public CPU getCpu() { return cpu; } public void setCpu(CPU cpu2) { this.cpu = cpu2; } }
package come.hanqi; public class Testcpu { //private static final String ICpu = null; public static void main(String[] args) { Computer zw=new Computer(); zw.setCpu(new CPU()); zw.cpu(); } }
运行结果:
已装cpu
性能良好
质量为优等
品牌为:因特尔
时间: 2024-10-11 13:43:08