求教一个面向对象的习题
原题:利用接口做参数,写个计算器,能完成+-*/运算1.定义一个接口ICompute含有一个方法int computer(int n,int m);
2.设计四个类分别实现次接口,完成+-*/运算;
3.设计一个类UseCompute含有方法:
public void useCom(ICompute com,int one,int two)
此方法要求能够
(1)用传递过来的对象调用computer方法完成运算
(2)输出运算的结果
测试调用UseCompute中的方法UseCom完成+-*/
==============================================
(以下命名空间为同一个,就不贴上来了)
interface ICompute
{
int Computer(int n,int m);
}
class Plus : ICompute
{
public override int Computer(int n,int m)
{
int plus = n + m;
return plus;
}
}
class Minus : ICompute
{
public override int Computer(int n, int m)
{
int minus = n - m;
return minus;
}
}
class Times : ICompute
{
public override int Computer(int n, int m)
{
int times = n * m;
return times;
}
}
class Devide : ICompute
{
public override int Computer(int n, int m)
{
int devide = n / m;
return devide;
}
}
==问题出在设计UseComputer类这里,我不太理解题意(ICompute类型的参数要传什么)
==帮忙看看问题在哪,或应该怎样设置这个类
class UseComputer : ICompute
{
public void useCom(ICompute com, int one, int two)
{
Plus plus = new Plus();
Minus minus = new Minus();
Times times = new Times();
Devide devide = new Devide();
if (com == plus)
(one, two);
else if (com == minus)
(one, two);
else if (com == times)
(one, two);
else if (com == devide)
(one,two);
}
}
//主程序
static void Main(string[] args)
{
UseComputer use = new UseComputer();
use.useCom(plus, 5, 8); //不知道题意是不是要这么应用,这里是报错
}