c# 设计一个类shape(抽象的),设计其两个子类,circle(圆形)类和square(正方形)类。利用多态实现

来源:百度知道 编辑:UC知道 时间:2024/06/07 06:29:21
初学。。。高手出招救救啊
c# 设计一个类shape(抽象的),设计其两个子类,circle(圆形)类和square(正方形)类。利用多态实现每个类的计算面积的方法,计算周长的方法。

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Shape[] shapes = new Shape[10];
for (int i = 0; i < 10; i++)
{
Random r = new Random();
if (i % 2 == 0)
{
shapes[i] = new Circle(r.NextDouble() * 10);
}
else {
shapes[i] = new Square(r.NextDouble() * 10);
}

shapes[i].Area();
shapes[i].Girth();
}
Console.ReadLine();
}
}

public abstract class Shape
{
//求面积
public abstract void Area();

//求周长
public abstract void Girth();
}

public class Circle : Shape
{
public const double Pi = 3.14;
//半径
public double R = 0.00;

public Circle(double r)
{
this.R = r;
Console.WriteLine("该形状是圆形.它的半径为:{0}",R);
}