定义一个接口IShape,其中包括方法Area()用来计算面积,方法Girth()用来计算周长。三角形类Triangle、矩形类Rectangle实现该接口,各形状所需属性自己定义,由构造方法设置初值,请编写完整程序分别求其面积和周长。
interface IShape
{
void Area();
void Girth();
}
class ITriangle : IShape
{
public void Area()
{
program p = new program();
Console.WriteLine("三角形");
Console.WriteLine("请输入边长");
p.L = double.Parse(Console.ReadLine());
Console.WriteLine("请输入高");
p.H = double.Parse(Console.ReadLine());
p.sum = 0.5 * p.L * p.H;
Console.WriteLine("三角形的面积:" + p.sum + "");
Console.ReadLine();
}
public void Girth()
{
program p = new program();
Console.WriteLine("三角形");
Console.WriteLine("请输入边长1");
p.L = double.Parse(Console.ReadLine());
Console.WriteLine("请输入边长2");
p.L1 = double.Parse(Console.ReadLine());
Console.WriteLine("请输入边长3");
p.L2 = double.Parse(Console.ReadLine());
p.sum = p.L + p.L1 + p.L2;
Console.WriteLine("三角形的周长:" + p.sum + "");
Console.ReadLine();
}
}
class ICircle : IShape
{
public void Area()
{
program p = new program();
Console.WriteLine("圆");
Console.WriteLine("请输入半径");
p.R = double.Parse(Console.ReadLine());
p.sum = 3.14 * p.R * p.R;
Console.WriteLine("圆形的面积:" + p.sum + "");
Console.ReadLine();
}
public void Girth()
{
program p = new program();
Console.WriteLine("圆");
Console.WriteLine("请输入半径");
p.R = double.Parse(Console.ReadLine());
p.sum = 2*3.14 * p.R;
Console.WriteLine("圆形的周长:" + p.sum + "");
Console.ReadLine();
}
}
class program
{
public double L, H, L1, L2, R,sum;
public program()
{
L = 0;
H = 0;
L1 = 0;
L2 = 0;
R = 0;
sum = 0;
}
static void Main(string[] args)
{
ICircle circle = new ICircle();
circle.Area();
circle.Girth();
ITriangle triangle = new ITriangle();
triangle.Area();
triangle.Girth();
}
}