using System;
class PruebaHerenciaFigura
{
public class Figura
{
public const double PI = Math.PI;
protected double x, y;
public Figura ()
{
}
public Figura (double x, double y)
{
this.x = x;
this.y = y;
}
public virtual double Area ()
{
return x * y;
}
}
public class Circulo : Figura
{
public Circulo (double r) : base (r, 0)
{
}
public override double Area ()
{
return PI * x * x;
}
}
public class Esfera : Figura
{
public Esfera (double r) : base (r, 0)
{
}
public override double Area ()
{
return 4 * PI * x * x;
}
}
public class Cilindro : Figura
{
public Cilindro (double r, double h) : base (r, h)
{
}
public override double Area ()
{
return 2 * PI * x * x + PI + x + y;
}
}
public static void Main ()
{
double r = 7.0, h = 11.0;
Figura[] figuras = new Figura[3];
figuras[0] = new Circulo (r);
figuras[1] = new Esfera (r);
figuras[2] = new Cilindro (r, h);
// Muestra resultados.
// La invocaciön del método Area ocurre polimófircamente
Console.WriteLine ("Área del círculo: {0:F2}", figuras[0].Area ());
Console.WriteLine ("Área de la esfera: {0:F2}", figuras[1].Area ());
Console.WriteLine ("Área del cilindro: {0:F2}", figuras[2].Area ());
}
}
dXNpbmcgU3lzdGVtOwoKY2xhc3MgUHJ1ZWJhSGVyZW5jaWFGaWd1cmEKewogICAgcHVibGljIGNsYXNzIEZpZ3VyYQogICAgewogICAgICAgIHB1YmxpYyBjb25zdCBkb3VibGUgUEkgPSBNYXRoLlBJOwogICAgICAgIHByb3RlY3RlZCBkb3VibGUgeCwgeTsKICAgICAgICAKICAgICAgICBwdWJsaWMgRmlndXJhICgpCiAgICAgICAgewogICAgICAgIH0KICAgICAgICAKICAgICAgICBwdWJsaWMgRmlndXJhIChkb3VibGUgeCwgZG91YmxlIHkpCiAgICAgICAgewogICAgICAgICAgICB0aGlzLnggPSB4OwogICAgICAgICAgICB0aGlzLnkgPSB5OwogICAgICAgIH0KICAgICAgICAKICAgICAgICBwdWJsaWMgdmlydHVhbCBkb3VibGUgQXJlYSAoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIHggKiB5OwogICAgICAgIH0KICAgIH0KICAgIAogICAgcHVibGljIGNsYXNzIENpcmN1bG8gOiBGaWd1cmEKICAgIHsKICAgICAgICBwdWJsaWMgQ2lyY3VsbyAoZG91YmxlIHIpIDogYmFzZSAociwgMCkKICAgICAgICB7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHB1YmxpYyBvdmVycmlkZSBkb3VibGUgQXJlYSAoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIFBJICogeCAqIHg7CiAgICAgICAgfQogICAgfQogICAgCiAgICBwdWJsaWMgY2xhc3MgRXNmZXJhIDogRmlndXJhCiAgICB7CiAgICAgICAgcHVibGljIEVzZmVyYSAoZG91YmxlIHIpIDogYmFzZSAociwgMCkKICAgICAgICB7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHB1YmxpYyBvdmVycmlkZSBkb3VibGUgQXJlYSAoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIDQgKiBQSSAqIHggKiB4OwogICAgICAgIH0KICAgIH0KICAgIAogICAgcHVibGljIGNsYXNzIENpbGluZHJvIDogRmlndXJhCiAgICB7CiAgICAgICAgcHVibGljIENpbGluZHJvIChkb3VibGUgciwgZG91YmxlIGgpIDogYmFzZSAociwgaCkKICAgICAgICB7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHB1YmxpYyBvdmVycmlkZSBkb3VibGUgQXJlYSAoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIDIgKiBQSSAqIHggKiB4ICsgUEkgKyB4ICsgeTsKICAgICAgICB9CiAgICB9CiAgICAKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBNYWluICgpCiAgICB7CiAgICAgICAgZG91YmxlIHIgPSA3LjAsIGggPSAxMS4wOwogICAgICAgIAogICAgICAgIEZpZ3VyYVtdIGZpZ3VyYXMgPSBuZXcgRmlndXJhWzNdOwogICAgICAgIAogICAgICAgIGZpZ3VyYXNbMF0gPSBuZXcgQ2lyY3VsbyAocik7CiAgICAgICAgZmlndXJhc1sxXSA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