public interface IDo { //创建接口 public void cpu(int s) ; //定义一个带参数的 方法 public boolean zuzhuang();//定义一个布尔型变量 }
public class diannao implements IDo { //创建一个类实现定义的接口 @Override public void cpu(int s) { System.out.println("cpu:"+s); } @Override public boolean zuzhuang() { System.out.println("组装成功"); return true; }
public class zuoye { private IDo diannao; public IDo getdiannao() { return diannao; } public void setDo(IDo diannao) { this.diannao = diannao; } int cpu ; boolean cpu() { boolean zz = true; if( cpu != 5){ System.out.println("组装未完成"); zz = false; } else { System.out.println("组装成功"); } return zz; } public static void main(String[] args) { //diannao cpu = new diannao(); zuoye mm = new zuoye(); mm.cpu = 5; if(mm.zuzhuang()){ System.out.println("组装成功"); } else{ System.out.println("未完成组装"); }
时间: 2025-01-09 14:34:56