C# 0 The Complete Reference


ob . Then, the contents of  ob



Download 4,07 Mb.
Pdf ko'rish
bet270/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   266   267   268   269   270   271   272   273   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

ob

. Then, the contents of 



ob

’s array are copied to the new 

array, and 

tos

 is set appropriately. After the constructor finishes, the new stack and the 

original stack are separate, but identical.

Invoke an Overloaded Constructor Through this

When working with overloaded constructors, it is sometimes useful for one constructor to 

invoke another. In C#, this is accomplished by using another form of the 

this

 keyword. The 

general form is shown here:

constructor-name

(

parameter-list1

) : this(

parameter-list2

) {


   // ... body of constructor, which may be empty

}

www.freepdf-books.com




198

 

P a r t   I :  



T h e   C #   L a n g u a g e

When the constructor is executed, the overloaded constructor that matches the parameter 

list specified by 

parameter-list2

is first executed. Then, if there are any statements inside the 

original constructor, they are executed. Here is an example:

// Demonstrate invoking a constructor through this.

using System;

class XYCoord {

  public int x, y;

  public XYCoord() : this(0, 0) {

    Console.WriteLine("Inside XYCoord()");

  }


  public XYCoord(XYCoord obj) : this(obj.x, obj.y) {

    Console.WriteLine("Inside XYCoord(obj)");

  }

  public XYCoord(int i, int j) {



    Console.WriteLine("Inside XYCoord(int, int)");

    x = i;

    y = j;

  }


}

class OverloadConsDemo {

  static void Main() {

    XYCoord t1 = new XYCoord();

    XYCoord t2 = new XYCoord(8, 9);

    XYCoord t3 = new XYCoord(t2);

    Console.WriteLine("t1.x, t1.y: " + t1.x + ", " + t1.y);

    Console.WriteLine("t2.x, t2.y: " + t2.x + ", " + t2.y);

    Console.WriteLine("t3.x, t3.y: " + t3.x + ", " + t3.y);

  }


}

The output from the program is shown here:

Inside XYCoord(int, int)

Inside XYCoord()

Inside XYCoord(int, int)

Inside XYCoord(int, int)

Inside XYCoord(obj)

t1.x, t1.y: 0, 0

t2.x, t2.y: 8, 9

t3.x, t3.y: 8, 9

Here is how the program works. In the 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   266   267   268   269   270   271   272   273   ...   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