38.利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用comp

//接口Compute
package jieKou;

public interface Compute {
int Computer(int n,int m);

}

//加
package jieKou;

public class Jia implements Compute {

@Override
public int Computer(int n, int m) {
	// TODO 自动生成的方法存根
	return m+n;
}

}

//减
package jieKou;

public class Jian implements Compute {

@Override
public int Computer(int n, int m) {
	// TODO 自动生成的方法存根
	return n-m;
}

}

//乘
package jieKou;

public class Cheng implements Compute {

@Override
public int Computer(int n, int m) {
	// TODO 自动生成的方法存根
	return n*m;
}

}

//除

package jieKou;

public class Chu implements Compute {

@Override
public int Computer(int n, int m) {
	// TODO 自动生成的方法存根
	return n/m;
}

}

//UseCompute

package jieKou;

public class UseCompute {
public void useCom(Compute com, int one, int two)
{
System.out.println(com.Computer(one, two));
}

}
//测试类

package jieKou;

public class Test001 {

public static void main(String[] args) {
	UseCompute a=new UseCompute();
	a.useCom(new Jia(), 5, 6);
	a.useCom(new Jian(), 5, 6);
	a.useCom(new Cheng(), 5, 6);
	a.useCom(new Chu(), 30, 6);
}

}

///运行结果

11
-1
30
5

原文地址:https://www.cnblogs.com/nicebaby/p/5903433.html