25.07.2017 Views

Intro-CSharp-Book-v2015

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Глава 14. Дефиниране на класове 557<br />

class Point<br />

{<br />

private double x;<br />

private double y;<br />

public Point(int x, int y)<br />

{<br />

this.x = x;<br />

this.y = y;<br />

}<br />

public double X<br />

{<br />

get { return x; }<br />

set { x = value; }<br />

}<br />

}<br />

public double Y<br />

{<br />

get { return y; }<br />

set { y = value; }<br />

}<br />

Полетата на обектите от нашия клас (т.е. координатите на точките) са<br />

декларирани като private и не могат да бъдат достъпвани чрез точкова<br />

нотация. Ако създадем обект от клас Point, можем да модифицираме и<br />

четем свойствата (координатите) на точката, единствено чрез свойствата<br />

за достъп до тях:<br />

using System;<br />

PointTest.cs<br />

class PointTest<br />

{<br />

static void Main()<br />

{<br />

Point myPoint = new Point(2, 3);<br />

double myPointXCoordinate = myPoint.X; // Access a property<br />

double myPointYCoordinate = myPoint.Y; // Access a property<br />

Console.WriteLine("The X coordinate is: " +<br />

myPointXCoordinate);<br />

Console.WriteLine("The Y coordinate is: " +<br />

myPointYCoordinate);<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!