Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_Prog_Guide.doc
Скачиваний:
16
Добавлен:
16.11.2019
Размер:
6.22 Mб
Скачать

Пример 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

Введенные данные

4

24

Результат 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

Interface Properties

Properties can be declared on an interface. The following is an example of an interface indexer accessor:

public interface ISampleInterface

{

// Property declaration:

string Name

{

get;

set;

}

}

The accessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.

Example

In this example, the interface IEmployee has a read-write property, Name, and a read-only property, Counter. The class Employee implements the IEmployee interface and uses these two properties. The program reads the name of a new employee and the current number of employees and displays the employee name and the computed employee number.

You could use the fully qualified name of the property, which references the interface in which the member is declared. For example:

string IEmployee.Name

{

get { return "Employee Name"; }

set { }

}

This is called Explicit Interface Implementation. For example, if the class Employee is implementing two interfaces ICitizen and IEmployee and both interfaces have the Name property, the explicit interface member implementation will be necessary. That is, the following property declaration:

string IEmployee.Name

{

get { return "Employee Name"; }

set { }

}