This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


// Use the get/set methods to interact with the object's name



Download 21,46 Mb.
Pdf ko'rish
bet357/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   353   354   355   356   357   358   359   360   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Use the get/set methods to interact with the object's name.

emp.SetName("Marv");

Console.WriteLine("Employee is named: {0}", emp.GetName());

Console.ReadLine();

}

Encapsulation Using Type Properties

Although you can encapsulate a piece of field data using traditional get and set methods, .NET lan-

guages prefer to enforce data protection using 

properties. First of all, understand that properties

always map to “real” accessor and mutator methods in terms of CIL code. Therefore, as a class

designer, you are still able to perform any internal logic necessary before making the value assign-

ment (e.g., uppercase the value, scrub the value for illegal characters, check the bounds of a

numerical value, and so on).

Here is the updated Employee class, now enforcing encapsulation of each field using property

syntax rather than traditional get and set methods:

class Employee

{

// Field data.



private string empName;

private int empID;

private float currPay;

// Properties.

public string Name

{

get { return empName; }



set { empName = value; }

}

public int ID



{

get { return empID; }

set { empID = value; }

}

public float Pay



{

get { return currPay; }

set { currPay = value; }

}

...



}

A C# property is composed by defining a get scope (accessor) and set scope (mutator) directly

within the property scope itself. Once we have these properties in place, it appears to the caller that

it is getting and setting a 



public point of data; however, the correct get and set block is called

behind the scenes to preserve encapsulation:

C H A P T E R   5  


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   353   354   355   356   357   358   359   360   ...   1830




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish