namespace Ex3._9
{
class Shape
{
protected double height;
protected double width;
public Shape()
{
height = width = 0;
}
public Shape(double x)
{
height = width = x;
}
public Shape(double h, double w)
{
height = h;
width = w;
}
public double area()
{
return height * width;
}
}
class Triangle : Shape
{
public Triangle(double x, double y) : base(x, y) { }
new public double area()
{
return height * width / 2;
}
}
class Trapezia : Shape
{
double width2;
public Trapezia(double w1, double w2, double h) : base(w1, h)
{
width2 = w2;
}
new public double area()
{
return (width + width2) * height /2;
}
class Program
{
static void Main(string[] args)
{
Shape A = new Shape(2, 4);
Triangle B = new Triangle(1, 2);
Trapezia C = new Trapezia(2, 3, 4);
Console.WriteLine("A.area()={0}", A.area());
Console.WriteLine("B.area()={0}", B.area());
Console.WriteLine("C.area()={0}", C.area());
A = B;
Console.WriteLine("A.area()={0}", A.area());
A = C;
Console.WriteLine("A.area()={0}", A.area());
Console.Read();
}
}
}
}
结果里C.area()=7
为什么不是C.area()=10,(2+3)*4/2=10?