C# 0 The Complete Reference


PART I C h a p t e r   1 2



Download 4,07 Mb.
Pdf ko'rish
bet411/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   407   408   409   410   411   412   413   414   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 2 :  

I n t e r f a c e s ,   S t r u c t u r e s ,   a n d   E n u m e r a t i o n s  

315


PART IPART I

  public int GetNext() {

    prev = val;

    val += 2;

    return val;

  }


  public void Reset() {

    val = start;

    prev = start - 2;

  }


  public void SetStart(int x) {

    start = x;

    val = start;

    prev = val - 2;

  }

  // A method not specified by ISeries.



  public int GetPrevious() {

    return prev;

  }

}

Notice that the addition of 



GetPrevious( )

 required a change to the implementations of the 

methods defined by 

ISeries

. However, since the interface to those methods stays the same, 

the change is seamless and does not break preexisting code. This is one of the advantages 

of interfaces.

As explained, any number of classes can implement an 

interface

. For example, here is a 

class called 

Primes

 that generates a series of prime numbers. Notice that its implementation 

of

ISeries

 is fundamentally different than the one provided by 



ByTwos

.

// Use ISeries to implement a series of prime numbers.



class Primes : ISeries {

  int start;

  int val;

  public Primes() {

    start = 2;

    val = 2;

  }

  public int GetNext() {



    int i, j;

    bool isprime;

    val++;

    for(i = val; i < 1000000; i++) {

      isprime = true;

      for(j = 2; j <= i/j; j++) {

        if((i%j)==0) {

          isprime = false;

          break;

        }

      }

www.freepdf-books.com




316

 

P a r t   I :  



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

      if(isprime) {

        val = i;

        break;

      }

    }


    return val;

  }


  public void Reset() {

    val = start;

  }

  public void SetStart(int x) {



    start = x;

    val = start;

  }

}

The key point is that even though 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   407   408   409   410   411   412   413   414   ...   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