星空网 > 软件开发 > Java

接口—计算器

利用接口做参数,写个计算器,能完成+-*/运算

(1)定义一个接口Compute含有一个方法int computer(int n,int m);

(2)设计四个类分别实现此接口,完成+-*/运算

(3)设计一个类UseCompute,含有方法:public void useCom(Compute com, int one, int two)

此方法要求能够:1.用传递过来的对象调用computer方法完成运算2.输出运算的结果

(4)设计一个测试类,调用UseCompute中的方法useCom来完成+-*/运算

1 public interface Compute {2   3   int computer(int m,int n);4 5 }

1 public class Add implements Compute {2 3   @Override4   public int computer(int m, int n) {5     6     return m+n;7   }8 9 }

1 public class Subtract implements Compute {2   3   @Override4   public int computer(int m, int n){5     6     return m-n;7   }8 9 }

1 public class Cheng implements Compute {2   3   @Override4   public int computer(int m, int n){5     6     return m*n;7   }8 9 }

1 public class Chu implements Compute {2 3   @Override4   public int computer(int m, int n) {5     6     return m/n;7   }8 9 }

 1 public class UseCompute { 2   public void useCom(Compute com, int one, int two) { 3  4     com.computer(one, two); 5     System.out.println("结果=" + com.computer(one, two)); 6  7   } 8  9   public static void main(String[] args) {10 11     UseCompute jsq = new UseCompute();12     // 加13     Add a = new Add();14     jsq.useCom(a, 150, 33);15     // 减16     Subtract b = new Subtract();17     jsq.useCom(b, 150, 33);18     // 乘19     Cheng c1 = new Cheng();20     jsq.useCom(c1, 150, 33);21     // 除22     Chu c2 = new Chu();23     jsq.useCom(c2, 150, 33);24   }25 }

结果:

接口—计算器




原标题:接口—计算器

关键词:接口

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

物流马来西亚海运:https://www.goluckyvip.com/tag/90976.html
马来西亚海运物流:https://www.goluckyvip.com/tag/90977.html
海运物流马来西亚:https://www.goluckyvip.com/tag/90978.html
马来西亚物流海运:https://www.goluckyvip.com/tag/90979.html
乌克兰进口:https://www.goluckyvip.com/tag/9098.html
物流海运马来西亚:https://www.goluckyvip.com/tag/90980.html
湘西游轮六 湘江游轮夜游:https://www.vstour.cn/a/411226.html
携程旅游网折扣优惠最新攻略(携程旅游网更佳折扣优惠):https://www.vstour.cn/a/411227.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流