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


// A class with a single public field



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

// A class with a single public field.

class Book

{

public int numberOfPages;



}

The problem with public field data is that the items have no ability to intrinsically “under-

stand” whether the current value to which they are assigned is valid with regard to the current

business rules of the system. As you know, the upper range of a C# int is quite large (2,147,483,647).

Therefore, the compiler allows the following assignment:

// Humm. That is one heck of a mini-novel!

static void Main(string[] args)

{

Book miniNovel = new Book();



miniNovel.numberOfPages = 30000000;

}

Although you have not overflowed the boundaries of an int data type, it should be clear that a



mini-novel with a page count of 30,000,000 pages is a bit unreasonable. As you can see, public fields

do not provide a way to trap logical upper (or lower) limits. If your current system has a business

rule that states a book must be between 1 and 1,000 pages, you are at a loss to enforce this program-

matically. Because of this, public fields typically have no place in a production-level class definition.

Encapsulation provides a way to preserve the integrity of an object’s state data. Rather than

defining public fields (which can easily foster data corruption), you should get in the habit of defin-

ing 

private data, which is indirectly manipulated using one of two main techniques:

• Define a pair of accessor (get) and mutator (set) methods.

• Define a type property.

Additionally, C# provides the readonly keyword, which also delivers a level of data protection.

Whichever technique you choose, the point is that a well-encapsulated class should hide the details

of how it operates from the prying eyes of the outside world. This is often termed 



black box pro-

gramming. The beauty of this approach is that an object is free to change how a given method is

implemented under the hood. It does this without breaking any existing code making use of it, pro-

vided that the signature of the method remains constant.


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   350   351   352   353   354   355   356   357   ...   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