计算器
1、工厂类 生产对象的类,
添加一个类 gongchang
//计算器
class jisuanqi
{
private int a;
public int A
{
get { return a; }
set { a = value; }
}
private int b;
public int B
{
get { return b; }
set { b = value; }
}
//加法
public int jia()
{
return a + b;
}
//减法
public int jian()
{
return a - b;
}
//乘法
public int cheng()
{
return a * b;
}
}
//改进计算器 先做一个父类
public class jisuan
{
private int a;
public int A
{
get { return a; }
set { a = value; }
}
private int b;
public int B
{
get { return b; }
set { b = value; }
}
public virtual int yunsuan()
{
return 0;
}
}
//j加法类 子类
public class jia : jisuan
{
public override int yunsuan()
{
return base.A + base.B;
}
}
//减法类
public class jian : jisuan
{
public override int yunsuan()
{
return base.A - base.B;
}
}
//工厂类
public class gongchang
{
public static jisuan duixiang(string s)
{
switch (s)
{
case "+":
return new jia();
break;
case "-":
return new jian();
break;
case "*":
return new chengfa ();
break;
default:
return new jia();
break;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数:");
int a=Convert.ToInt32( Console.ReadLine());
Console.WriteLine("请输入第二个数:");
int b = Convert.ToInt32(Console.ReadLine());
jisuan c = gongchang.duixiang("+"); 只改这里
c.A = a;
c.B = b;
Console.WriteLine("两个数的积为:"+c.yunsuan());
Console.ReadLine();
}
}
class chengfa:jisuan
{
public override int yunsuan()
{
return base.A*base.B;
}
}
------