Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Методичка для КР по ООП.doc
Скачиваний:
8
Добавлен:
18.04.2019
Размер:
2.47 Mб
Скачать

Пример. Свойства и индексаторы

using System;

namespace PointModel

{

class SimplePoint

{//=============================================================================

float x, y;

public SimplePoint[] arraySimplePoint = null;

public SimplePoint()

{

x = 0.0F;

y = 0.0F;

}

public SimplePoint(float xKey, float yKey): this()

{

x = xKey;

y = yKey;

}

public SimplePoint(SimplePoint PointKey):this(PointKey.x, PointKey.y)

{

}

public SimplePoint(int N)

{

int i;

if (N > 0 && arraySimplePoint == null)

{

arraySimplePoint = new SimplePoint[N];

for (i = 0; i < N; i++)

{

arraySimplePoint[i] = new SimplePoint((float)i, (float)i);

}

}

}

}//=============================================================================

class MyPoint

{//=============================================================================

float x, y;

MyPoint[] arrayMyPoint = null;

int arrLength = 0;

int ArrLength

{

get

{

return arrLength;

}

set

{

arrLength = value;

}

}

bool isArray = false;

bool IsArray // Это свойство только для чтения!

{

get

{

return isArray;

}

}

MyPoint()

{

x = 0.0F;

y = 0.0F;

}

public MyPoint(float xKey, float yKey): this()

{

x = xKey;

y = yKey;

}

public MyPoint(MyPoint PointKey): this(PointKey.x, PointKey.y)

{

}

public MyPoint(int N)

{

int i;

if (N > 0 && IsArray == false)

{

this.isArray = true;

this.arrLength = N;

arrayMyPoint = new MyPoint[N];

for (i = 0; i < N; i++)

{

arrayMyPoint[i] = new MyPoint((float)i, (float)i);

}

}

}

bool InArray(int index)

{

if (!IsArray) return false;

if (index >= 0 && index < this.ArrLength) return true;

else return false;

}

// Объявление операторной функции индексации

public MyPoint this[int index]

{

get

{

if (IsArray == false) return null;

if (InArray(index)) return arrayMyPoint[index];

else return null;

}

set

{

if (IsArray == false) return;

if (InArray(index))

{

arrayMyPoint[index].x = value.x;

arrayMyPoint[index].y = value.y;

}

else return;

}

}

// Объявление ещё одной (перегруженной) операторной функции индексации.

// В качестве значения для индексации используется символьная строка!

public MyPoint this[string strIndex]

{

get

{

int index = int.Parse(strIndex);

if (IsArray == false) return null;

if (InArray(index)) return arrayMyPoint[index];

else return null;

}

set

{

int index = int.Parse(strIndex);

if (IsArray == false) return;

if (InArray(index))

{

arrayMyPoint[index].x = value.x;

arrayMyPoint[index].y = value.y;

}

else return;

}

}

}//=============================================================================

class Class1

{

static void Main(string[] args)

{

SimplePoint spArr = new SimplePoint(8);

SimplePoint wsp = new SimplePoint(3.14F, 3.14F);

SimplePoint wsp0;

spArr.arraySimplePoint[3] = wsp;

try

{

spArr.arraySimplePoint[125] = wsp;

}

catch (IndexOutOfRangeException exc)

{

Console.WriteLine(exc);

}

try

{

wsp.arraySimplePoint[7] = wsp;

}

catch (NullReferenceException exc)

{

Console.WriteLine(exc);

}

try

{

wsp0 = spArr.arraySimplePoint[125];

}

catch (IndexOutOfRangeException exc)

{

Console.WriteLine(exc);

}

wsp0 = spArr.arraySimplePoint[5];

//========================================================================

MyPoint mpArr = new MyPoint(10);

MyPoint wmp = new MyPoint(3.14F, 3.14F);

MyPoint wmp0;

mpArr[3] = wmp;

mpArr[125] = wmp;

wmp[7] = wmp;

wmp0 = mpArr[125];

wmp0 = mpArr[5];

// В качестве индексатора используются строковые выражения.

wmp0 = mpArr["5"];

// В том числе, использующее конкатенацию строк.

wmp0 = mpArr["1"+"2"+"5"];

}

}

}