\定义一个抽象类Shape,它有两个抽象的方法area()和perimeter(),分别表示求面积和周长.

如题所述

简单工厂模式?
我之前做了个简易计算器的简单工厂模式,效果与你这个是一样的~~~~
你的抽象类Shape里面的方法area()和perimeter(),可以参考的计算器的GetResult()
都是虚方法(使用virtual 关键字),然后在计算面积、周长的子类中分别重写(override关键字)就是了~~~~~~~~~

具体的你看我的 简易计算器工程模式 代码:

C# 简单工厂之简易计算器
总共三个类:Operation.cs OperationFactory.cs Program.cs
Operation.cs 两个操作数,一个运算的虚方法GetResult(),该方法得出运算结果
加减乘除 四个运算类,每个类继承于Operation.cs,每个类都有一个GetResult()方法
,子类的GetResult()用于实现具体的加减乘除运算~~~~~~~~
OperationFactory.cs 实例化 加减乘除 四个运算类~~~~~
Program.cs 里面是main()调用运算类~~~~~

Operation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
/// <summary>
/// 运算类
/// </summary>
public class Operation
{
#region 运算类,包含两个操作数,一个虚的运算方法

private double _numberA = 0;
/// <summary>
/// 操作数A
/// </summary>
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}

private double _numberB = 0;
/// <summary>
/// 操作数B
/// </summary>
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}

#region 虚方法,得到运算结果
public virtual double GetResult()
{
double result = 0;
return result;
}
#endregion

#endregion
}

/// <summary>
/// 加法类:继承于运算类
/// </summary>
public class OperationAdd : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}

/// <summary>
/// 减法类:继承于运算类
/// </summary>
public class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}

/// <summary>
/// 乘法类:继承于运算类
/// </summary>
public class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
/// <summary>
/// 除法类:继承于运算类
/// </summary>
public class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB==0)
{
throw new Exception("除数不能为零!");

}
else
{
result = NumberA / NumberB;
return result;
}

}
}

#region 简单工厂模式结论
/* 若要实现复杂的运算,比如开根号、自然对数、求正余弦等等,
* 只需要:
* (1)增加相应的运算子类
* (2)在工厂的switch子句中添加相应复杂运算的分支
* 就OK了
*/
#endregion
}

OperationFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
public class OperationFactory
{
/// <summary>
/// 根据相应的运算符号,工厂就实例化出合适的运算对象
/// </summary>
/// <param name="operationType">加减乘除等运算符号</param>
/// <returns></returns>
public static Operation createOperation(string operationType)
{
Operation operation = null;
//分别实例化具体的 加减乘除 运算类~~~~~~~~~
switch (operationType)
{
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/" :
operation = new OperationDiv();
break;
default:
operation = new Operation();
break;
}
return operation;
}
}
}

Program.cs
调用~~~~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
class Program
{
static void Main(string[] args)
{
double result = 0;
string operationType = "+";
Operation oper = null;

// 【传入运算符,工厂实例化创造一个相应的运算对象】
oper = OperationFactory.createOperation(operationType);
oper.NumberA = 1;
oper.NumberB = 5;
// 【经过多态,返回父类的方式,实现计算结果】
result = oper.GetResult();
Console.WriteLine(result);

operationType = "-";
oper = OperationFactory.createOperation(operationType);
oper.NumberA = 1;
oper.NumberB = 5;
result = oper.GetResult();
Console.WriteLine(result);

operationType = "*";
oper = OperationFactory.createOperation(operationType);
oper.NumberA = 1;
oper.NumberB = 5;
result = oper.GetResult();
Console.WriteLine(result);

operationType = "/";
oper = OperationFactory.createOperation(operationType);
oper.NumberA = 1;
oper.NumberB = 5;
result = oper.GetResult();
Console.WriteLine(result);

Console.ReadKey();
}
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-11-20
#include<iostream>
#define PI 3.1415926;
using namespace std;
class Shape //抽象类的 定义
{
public:
virtual double GetArea() = 0; //纯虚函数
virtual double GetPerim() = 0; //纯虚函数
};

class Rectangle : public Shape //矩形类,公有继承
{
public: Rectangle(double aa, double bb) //带参数的 构造函数
{
a=aa;
b=bb;

}
virtual double GetArea()
{
return a * b;
}
virtual double GetPerim()
{
return 2*( a + b );
}
private:
double a;
double b;
};

class Circle : public Shape //圆类,公有继承
{
public: Circle(double rr) //带参数的 构造函数
{
r=rr;

}
virtual double GetArea()
{
return r * r * PI;
}
virtual double GetPerim()
{
return 2 * r * PI;
}
private:
double r;
};

void main()
{
double length, width;
cout << "输入长和宽: ";
cin >> length >> width;
Rectangle rect(length, width);
cout << "面积是:"<< rect.GetArea() << endl<<"周长是:"<<rect.GetPerim()<<endl;

double rr;
cout << "输入半径: ";
cin >> rr;
Circle cir(rr);
cout << "面积是:"<<cir.GetArea() << endl<<"周长是:"<<cir.GetPerim()<<endl;
}
第2个回答  2011-11-18
你是东职网络班的吧
相似回答