c# 方法的重构
namespace shape{
class MyFormatHelper
{
/// <summary>
/// 打印操作成功或失败消息
/// </summary>
public static void AlertTips(string tips)
{
Console.WriteLine("******************************************************");
Console.WriteLine("* {0} *", tips);
Console.WriteLine("******************************************************");
}
}
/// <summary>
/// 基类形状
/// </summary>
class shape
{
public const double PI=3.14;
/// <summary>
/// 抽象方法画图
/// </summary>
public virtual void DrawShape(){}
}
/// <summary>
/// 圆类,继承了形状类
/// </summary>
class Circle : shape
{
/// <summary>
/// 覆盖了画图方法
/// </summary>
public override void DrawShape()
{
Console.WriteLine("我画了一个圆");
}
/// <summary>
/// 求面积
/// </summary>
public void GetArea(double r)
{
Console.WriteLine("圆的面积是{0}",PI * r );
}
}
/// <summary>
/// 矩形类,继承了形状类
/// </summary>
class Rectange : shape
{
/// <summary>
/// 覆盖了画图方法
/// </summary>
public override void DrawShape()
{
Console.WriteLine("我画了一个矩形");
}
/// <summary>
/// 求面积
/// </summary>
public void GetArea(double lenght ,double width)
{
Console.WriteLine("矩形的面积是{0}", lenght * width);
}
}
/// <summary>
/// 三角形类,继承了形状类
/// </summary>
class Triangle : shape
{
/// <summary>
/// 覆盖了画图方法
/// </summary>
public override void DrawShape()
{
Console.WriteLine("我画了一个三角形");
}
/// <summary>
/// 求面积
/// </summary>
public void GetArea(double a, double b, double c, double s)
{
Console.WriteLine("三角形的面积是{0}", Math .Sqrt(s * (s - a) * (s - b) *
(s - c)));
}
}
/// <summary>
/// 梯形类,继承了形状类
/// </summary>
class Echelon : shape
{
/// <summary>
/// 覆盖了画图方法
/// </summary>
public override void DrawShape()
{
Console.WriteLine("我画了一个梯形");
}
/// <summary>
/// 求面积
/// </summary>
public void GetArea(double width, double lenght, double height)
{
Console.WriteLine("梯形的面积是{0}", (width + lenght) * height / 2);
}
}
/// <summary>
/// 信息类,打印画图
/// </summary>
class Info
{
public void GetInfo(params shape[] shape)
{
foreach (shape s in shape)
{
s.DrawShape();
}
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请选则操作:");
Console.WriteLine("------------------------------------------------");
Console.WriteLine("1: 圆");
Console.WriteLine("2: 矩形 ");
Console.WriteLine("3: 三角形 ");
Console.WriteLine("4: 梯形");
Console.WriteLine("0、退出");
int intOpCategory = 0;
bool flag = true;
Circle c = new Circle();
Rectange r = new Rectange();
Triangle t = new Triangle();
Echelon e = new Echelon();
Info info = new Info();
while (flag)
{
try
{
intOpCategory = Convert.ToInt32(Console.ReadLine());
switch (intOpCategory)
{
case 0:
flag = false;
break;
case 1:
info.GetInfo(c);
Console.WriteLine("请输入圆的半径");
c.GetArea(Convert.ToDouble(Console.ReadLine()));
break;
case 2:
info.GetInfo(r);
Console.WriteLine("请输入矩形的长与宽");
double lenght = Convert.ToDouble(Console.ReadLine());
double width = Convert.ToDouble(Console.ReadLine());
r.GetArea(lenght, width);
break;
case 3:
info.GetInfo(t);
Console.WriteLine("请分别输入输入三角形的边长");
double a = Convert.ToDouble(Console.ReadLine());
double b = Convert.ToDouble(Console.ReadLine());
double d = Convert.ToDouble(Console.ReadLine());
double s = Convert.ToDouble(Console.ReadLine());
t.GetArea(a, b, d, s);
break;
case 4:
info.GetInfo(e);
Console.WriteLine("请输入梯形的长宽与高");
double ewidth = Convert.ToDouble(Console.ReadLine());
double eheight = Convert.ToDouble(Console.ReadLine());
double elenght = Convert.ToDouble(Console.ReadLine());
e.GetArea(ewidth, eheight, elenght);
break;
default:
MyFormatHelper.AlertTips("所选操作不正确,请重新选择!");
break;
}
}
catch
{
Console.WriteLine("输入格式不正确,请重新输入");
}
}
}
}
这是我写的类,但是我觉得是否有更好的方法来重构呢,因为每一个方法都有一个求面积的方法。我原先想过吧求面积方法写成抽象方法,传不同的参数,但是失败了,好像对于不同的参数不怎么好设计。请大家帮一下忙,帮我解决一下。先谢谢了 --------------------编程问答-------------------- 不会 帮顶一下~~~~~~~~~~~~~~~~~~~
补充:.NET技术 , C#