日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[基础题]2.(*)利用接口做参数,写个计算器,能完成加减乘除运算。

發布時間:2024/6/3 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [基础题]2.(*)利用接口做参数,写个计算器,能完成加减乘除运算。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*2.(*)利用接口做參數,寫個計算器,能完成加減乘除運算。
(1)定義一個接口Compute含有一個方法int computer(int n, int m)。
(2)設計四個類分別實現此接口,完成加減乘除運算。
(3)設計一個類UseCompute,類中含有方法:public void useCom(Compute com, int one, int two),此方法能夠用傳遞過來的對象調用computer方法完成運算,并輸出運算的結果。
(4)設計一個主類Test,調用UseCompute中的方法useCom來完成加減乘除運算

*/

package HomeWork_10; public class Test_02 {//(4)public static void main(String[] args) {UseCompute uc = new UseCompute();Add s1 = new Add();Sub s2 = new Sub();Mul s3 = new Mul();Div s4 = new Div();System.out.print("和:");uc.useCom(s1,4,2);System.out.print("差:");uc.useCom(s2,4,2);System.out.print("積:");uc.useCom(s3,4,2);System.out.print("商:");uc.useCom(s4,4,2);} }interface Compute{//(1)接口int compute(int n,int m); }class Add implements Compute{//(2)public int compute(int n,int m){return n+m ;} }class Sub implements Compute{//(2)public int compute(int n,int m){return n-m ;} } class Mul implements Compute{//(2)public int compute(int n,int m){return n*m;} } class Div implements Compute{//(2)public int compute (int n,int m){return n/m;} }class UseCompute{//(3) Compute com就是Compute com =new Compute(); com.compute//new個com對象,對象調用方法,把one和two傳參進去public void useCom(Compute com, int one, int two){//方法名,傳參System.out.println(com.compute(one,two));} }

總結

以上是生活随笔為你收集整理的[基础题]2.(*)利用接口做参数,写个计算器,能完成加减乘除运算。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。