1)、代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace shap
{
public class shap
{
public virtual void MyArea()
{
Console.WriteLine("no use");
}
}
public class circle : shap
{
double r, carea;
public circle(double r)
{
this.r = r;
}
public override void MyArea()
{
carea = Math.PI * r * r;
Console.WriteLine("该圆形的面积为{0}", carea);
}
}
public class triangle : shap
{
double tarea, hemiline, h;
public triangle(double hemiline, double h)
{
this.hemiline = hemiline;
this.h = h;
}
public override void MyArea()
{
tarea = hemiline * h / 2;
Console.WriteLine("hemiline={0},h={1}", hemiline, h);
Console.WriteLine("该圆形的面积为{0}", tarea);
}
}
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("请输入你选择你要计算的图形类型:");
Console.WriteLine("1.circle(圆形面积)");
Console.WriteLine("2.triangle(三角形的面积)");
int s1 = Convert.ToInt32(Console.ReadLine());
switch (s1)
{
case 1:
Console.WriteLine("请输入圆的半径r:");
int r;
r = Int32.Parse(Console.ReadLine());
shap MyShape;
MyShape = new circle(r);
MyShape.MyArea();
break;
case 2:
Console.WriteLine("请输入三角形的高h,宽íhemiline:");
int h, hemiline;
h = Int32.Parse(Console.ReadLine());
hemiline = Int32.Parse(Console.ReadLine());
MyShape = new triangle(hemiline, h);
MyShape.MyArea();
break;
default:
Console.WriteLine("您输入有误,请重新输入!");
break;
}
}
}
}
}
2)、实验结果截图: