本帖最后由 海枫 于 2014-5-20 21:46 编辑
class Program
{
/*27、使用抽象类来实现,计算圆形和矩形的面积和周长。要求:(写一个”Shape”类作为圆形和矩形的父类,
* 提供两个抽象方法,分别用来计算周长和面积,子类圆形和矩形分别在自己的类中重写父类的抽象方法)*/
static void Main(string[] args)
{
Console.WriteLine("请输入圆的半径");
string r = Console.ReadLine();
double r1 = Convert.ToDouble(r);
Circle circle = new Circle(r1);
circle.Area();
circle.Circumference();
Console.WriteLine("请输入矩形的长");
double chang = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("请输入矩形的宽");
double kuan = Convert.ToDouble(Console.ReadLine());
Rectangle rectangle = new Rectangle(chang, kuan);
rectangle.Area();
rectangle.Circumference();
Console.ReadKey();
}
}
abstract class Shape
{
public abstract void Area();
public abstract void Circumference();
}
class Circle : Shape
{
double pi = Math.PI;
double r, s, c;
public Circle(double r)
{
this.r = r;
}
public override void Area()
{
s = pi * r * r;
Console.WriteLine("圆的面积是{0}", s);
}
public override void Circumference()
{
c = 2 * pi * r;
Console.WriteLine("圆的周长是{0}", c);
}
}
class Rectangle : Shape
{
double a, b, s, c;
public Rectangle(double a, double b)
{
this.a = a;
this.b = b;
}
public override void Area()
{
s = a * b;
Console.WriteLine("矩形的面积是{0}", s);
}
public override void Circumference()
{
c = 2 * (a + b);
Console.WriteLine("矩形的周长是{0}", c);
}
} |