介面—計算機,介面計算機

來源:互聯網
上載者:User

介面—計算機,介面計算機

利用介面做參數,寫個計算機,能完成+-*/運算

(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 }

結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.