利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用compute
package com.homework5; public interface Compute { //声明抽象方法 int computer(int n,int m); }
package com.homework5; public class jia implements Compute { @Override public int computer(int n, int m) { return n+m; } }
package com.homework5; public class jian implements Compute { @Override public int computer(int n, int m) { return n-m; } }
package com.homework5; public class cheng implements Compute { @Override public int computer(int n, int m) { return n*m; } }
package com.homework5; public class chu implements Compute { @Override public int computer(int n, int m) { return n/m; } }
package com.homework5; public class UseCompute { public void useCom(Compute com, int one, int two) { System.out.println(com.computer(one, two)); } }
package com.homework5; public class E { public static void main(String[] args) { UseCompute uc= new UseCompute(); uc.useCom(new jia(), 5, 8); uc.useCom(new jian(), 2, 8); uc.useCom(new cheng(), 5, 8); uc.useCom(new chu(), 40, 8); } }