Use the interface to do the parameters, write a calculator, can complete the +-*/operation
(1) Define an interface compute contains a method int computer (int n,int m);
(2) The design of four classes to implement this interface, complete +-*/operation
(3) The design of a class Usecompute, containing methods:
public void usecom (Compute com, int one, int)
This method requires the ability to: 1. Call the computer method with the passed object to complete the operation.
2. The result of the output operation
(4) Design a test class, call the method usecom in Usecompute to complete the +-*/operation
1. Interface
Package Lianxi4;public interface Computer {int computer (int n,int m);}
2. Calculation method
Package Lianxi4;public class Jiafa implements computer {@Overridepublic int computer (int n, int m) {return m+n;}}
Package Lianxi4;public class Jianfa implements computer {@Overridepublic int computer (int n, int m) {//TODO auto-generated method stub Retu RN n-m;}}
Package Lianxi4;public class Chengfa implements computer {@Overridepublic int computer (int n, int m) {return n*m;}}
Package Lianxi4;public class Chufa implements computer {@Overridepublic int computer (int n, int m) {//TODO auto-generated method stub retur n n/m;}}
3. Definition method
Package Lianxi4;public class Usecomputer {//(3) Design a class usecompute, containing a method://public void usecom (Compute com, int one, int two)// This method requires the ability to: 1. Call the computer method with the passed object to complete the operation// 2. The result of the output operation is public void usecom (computer com, int one, int) {SYSTEM.OUT.PRINTLN (Com.computer (one, both));}}
4. Testing and Results
Package Lianxi4;public class Text {public static void main (string[] args) {usecomputer u=new usecomputer (); Jiafa a=new Jiafa (); System.out.print ("The result of the addition operation of the 3+4 is:"); U.usecom (A, 3, 4); Jianfa b=new Jianfa (); System.out.print ("7-2 of the subtraction operation result is:"); U.usecom (b, 7, 2); CHENGFA c=new Chengfa (); System.out.print (the result of the multiplication operation of the 3x4 is: "); U.usecom (c, 3, 4); Chufa d=new Chufa (); System.out.print ("9/3 of the division operation result is:"); U.usecom (d, 9, 3);}}
Java Basic interface--operations