C# 0 The Complete Reference


PART I C h a p t e r   8



Download 4,07 Mb.
Pdf ko'rish
bet286/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   282   283   284   285   286   287   288   289   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   8 :  

A   C l o s e r   L o o k   a t   M e t h o d s   a n d   C l a s s e s  

209


PART IPART I

Current count: 7

Current count: 8

Current count: 9

Current count: 10

Each time that an object of type 



CountInst

 is created, the 



static

 field 


count

 is incremented. 

Each time an object is recycled, 

count

 is decremented. Thus, 



count

 always contains a count of 

the number of objects currently in existence. This is possible only through the use of a 

static

field. There is no way for an instance variable to maintain the count because the count relates 

to the class as a whole, not to a specific instance.

Here is one more example that uses 



static

. Earlier in this chapter, you saw how a class 

factory could be used to create objects. In that example, the class factory was a non-

static

method, which meant that it could be called only through an object reference. This meant 

that a default object of the class needed to be created so that the factory method could be 

called. However, a better way to implement a class factory is as a 



static

 method, which 

allows the class factory to be called without creating an unnecessary object. Here is the 

class factory example rewritten to reflect this improvement:

// Use a static class factory.

using System;

class MyClass {

  int a, b;

  // Create a class factory for MyClass.

  static public MyClass Factory(int i, int j) {

    MyClass t = new MyClass();

    t.a = i;

    t.b = j;

    return t; // return an object

  }

  public void Show() {



    Console.WriteLine("a and b: " + a + " " + b);

  }


}

class MakeObjects {

  static void Main() {

    int i, j;

    // Generate objects using the factory.

    for(i=0, j=10; i < 10; i++, j--) {

      MyClass ob = MyClass.Factory(i, j); // get an object

      ob.Show();

    }

    Console.WriteLine();



  }

}

www.freepdf-books.com




210

 

P a r t   I :  



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

In this version, 



Factory( )

 is invoked through its class name in this line of code:

MyClass ob = MyClass.Factory(i, j); // get an object

There is no need to create a 



MyClass

 object prior to using the factory.




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   282   283   284   285   286   287   288   289   ...   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