using System;
using System.Collections;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args) //方法必须包含在类中;程序的编译和运行从main()方法开始;
{ Shape[] shapes = new Shape[3];
shapes[0] = new Circle();
shapes[1] = new Rect();
shapes[2] = new Square();
for (int i = 0; i < shapes.Length; i++)
{
shapes[i].area(10, 5);
shapes[i].girth(10, 5);
}
}
}
class Shape
{
public virtual void area(int a,int b)
{
}
public virtual void girth(int a,int b)
{
}
}
class Circle:Shape
{
const float PI = 3.14f;
public override void area(int r,int b)
{
Console.WriteLine("半径为:"+r+"圆的面积:" + PI*r*r);
}
public override void girth(int r,int b)
{
Console.WriteLine("半径为:" + r + " 圆的周长:" +PI*2*r);
}
}
class Rect:Shape
{
public override void area(int length,int width)
{
Console.WriteLine("长:{0} 宽:{1} 长方形的面积:{2}",length,width, length*width);
}
public override void girth(int length, int width)
{
Console.WriteLine("长:{0} 宽:{1} 长方形的周长:{2}" ,length,width,(length+width)*2);
}
}
class Square :Rect
{
public override void area(int length, int width)
{
Console.WriteLine("边长"+length+"正方形的面积:" + length*length);
}
public override void girth(int length, int width)
{
Console.WriteLine("边长" + length + "正方形的周长:" + length*4);
}
}
}
C#2022/11/23
最新推荐文章于 2024-09-07 20:15:38 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)