2016/1/21 练习 创建 接口interface 应用implements 类class 并实例化调用
1 package testinterface; 2 3 public interface ICpu { 4 //电压 5 public boolean dianya(); 6 //控制 7 public void kongzhi(); 8 //运算 9 public void yunsuan(); 10 //存储 11 public void cunchu(); 12 13 }
1 public class Intel implements ICpu{ 2 3 @Override 4 public boolean dianya() { 5 System.out.println("电压正常"); 6 return true; 7 } 8 9 @Override 10 public void kongzhi() { 11 System.out.println("控制单元工作"); 12 13 } 14 15 @Override 16 public void yunsuan() { 17 System.out.println("运算单元工作"); 18 19 } 20 21 @Override 22 public void cunchu() { 23 System.out.println("存储单元工作"); 24 25 }
1 package testinterface; 2 import testinterface.ICpu; 3 4 5 public class Computer { 6 7 private ICpu cpu; 8 9 public ICpu getCpu() { 10 return cpu; 11 } 12 13 public void setCpu(ICpu intel) { 14 this.cpu = intel; 15 16 String i="intel"; 17 if( !i.equals("intel")){ 18 System.out.println("cpu不可以使用"); 19 20 } 21 else if (i.equals("intel")){ 22 System.out.println("cpu可以使用"); 23 } 24 else{ 25 26 } 27 28 29 } 30 31 32 33 34 }
调用
1 package testinterface; 2 3 public class Testcomputer { 4 5 public static void main(String[] args) { 6 Computer in=new Computer(); 7 in.setCpu(new Intel()); 8 9 } 10 }