logo
CSharp_Prog_Guide

Пример 3 Описание

В данном примере два класса Cube и Square реализуют абстрактный класс Shape и переопределяют его абстрактное свойство Area. Обратите внимание на использование для свойств модификатора override. Программа принимает размер стороны в качестве входных данных и вычисляет площади квадрата и куба. Она также принимает площадь в качестве входных данных и вычисляет значение стороны квадрата и куба.

Код

-----

class Cube : Shape

{

public double side;

public Cube(double s)

{

side = s;

}

public override double Area

{

get

{

return 6 * side * side;

}

set

{

side = System.Math.Sqrt(value / 6);

}

}

}

class TestShapes

{

static void Main()

{

// Input the side:

System.Console.Write("Enter the side: ");

double side = double.Parse(System.Console.ReadLine());

// Compute the areas:

Square s = new Square(side);

Cube c = new Cube(side);

// Display the results:

System.Console.WriteLine("Area of the square = {0:F2}", s.Area);

System.Console.WriteLine("Area of the cube = {0:F2}", c.Area);

System.Console.WriteLine();

// Input the area:

System.Console.Write("Enter the area: ");

double area = double.Parse(System.Console.ReadLine());

// Compute the sides:

s.Area = area;

c.Area = area;

// Display the results:

System.Console.WriteLine("Side of the square = {0:F2}", s.side);

System.Console.WriteLine("Side of the cube = {0:F2}", c.side);

}

}

--------

Input

4

24

Output 3

Enter the side: 4

Area of the square = 16.00

Area of the cube = 96.00

Enter the area: 24

Side of the square = 4.90

Side of the cube = 2.00