C# 0 The Complete Reference


PART I C h a p t e r   6



Download 4,07 Mb.
Pdf ko'rish
bet190/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   186   187   188   189   190   191   192   193   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   6 :  

I n t r o d u c i n g   C l a s s e s   a n d   O b j e c t s  

127


PART IPART I

house.Occupants = 4;

house.Area = 2500;

house.Floors = 2;

An approach like this would never be used in professionally written C# code. Aside from 

this approach being error prone (you might forget to set one of the fields), there is simply a 

better way to accomplish this task: the constructor.

A

constructor

 initializes an object when it is created. It has the same name as its class and 

is syntactically similar to a method. However, constructors have no explicit return type. The 

general form of a constructor is shown here:

access class

-

name

(

param-list

) {


   // constructor code

}

Typically, you will use a constructor to give initial values to the instance variables defined 



by the class or to perform any other startup procedures required to create a fully formed 

object. Also, usually, 



access

 is 


public

 because constructors are normally called from outside 

their class. The 

param-list

 can be empty, or it can specify one or more parameters.

All classes have constructors, whether you define one or not, because C# automatically 

provides a default constructor that causes all member variables to be initialized to their 

default values. For most value types, the default value is zero. For 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   186   187   188   189   190   191   192   193   ...   1096




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