1.声明一个图形基类Shape,该类拥有用于存储Shape面积的属性S,以及用于计算面积的虚方法GetArea,由Shape类派生出Rectangle和Circle类,并在子类中重写GetArea方法分别用于计算矩形和圆形的面积,在主函数中完成派生类的实例化及其测试。
2.基类Shape类是一个表示形状的抽象类,该类拥有用于存储Shape面积的属性Area,以及用于求面积的抽象方法GetArea。请从Shape类派生三角形类(),并给出具体的求面积函数GetArea,在主函数中完成类的实例化及其测试。
```c
namespace
{
public class Shape
{
private float s;
public float S
{
get
{
return s;
}
set
{
s = value;
}
}
public virtual void GetArea()
{
}
}
public class Rectangle : Shape
{
public float x;
public float y;
public override void GetArea()
{
Console.WriteLine("输入矩形长和宽");
float x = float.Parse(Console.ReadLine());
float y = float.Parse(Console.ReadLine());
this.S = x * y;
}
}
class Circle : Shape
{
static float pi = 3.1415926f;
public float r;
public override void GetArea()
{
Console.WriteLine("输入圆的半径r:");
float r = float.Parse(Console.ReadLine());
this.S =pi * r * r;
}
}
class Program
{
static void Main(string[] args)
{
Rectangle rectangle = new Rectangle();
rectangle.GetArea();
Console.WriteLine("矩形的面积:{0}",rectangle.S);
Circle circle = new Circle();
circle.GetArea();
Console.WriteLine("圆形的面积:{0}", circle.S);
}
}
}
```c
namespace 作业3_2
{
abstract class Shape
{
public double s;
public abstract void GetArea();
public double Area { get; set; }
}
class Triangle : Shape
{
public override void GetArea()
{
Console.WriteLine("输入三角形底和高");
float h = float.Parse(Console.ReadLine());
float d = float.Parse(Console.ReadLine());
this.Area = h * d * 1 / 2;
}
public new double Area
{
get
{
return s;
}
set
{
s = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Triangle triangle = new Triangle();
triangle.GetArea();
Console.WriteLine("三角形的面积:{0}", triangle.Area);
}
}
}