C# 0 The Complete Reference


PART I C h a p t e r   1 2



Download 4,07 Mb.
Pdf ko'rish
bet413/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   409   410   411   412   413   414   415   416   ...   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  

317


PART IPART I

  public ByTwos() {

    start = 0;

    val = 0;

  }

  public int GetNext() {



    val += 2;

    return val;

  }

  public void Reset() {



    val = start;

  }


  public void SetStart(int x) {

    start = x;

    val = start;

  }


}

// 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;

        }

      }

      if(isprime) {

        val = i;

        break;

      }

    }


    return val;

  }


  public void Reset() {

    val = start;

  }

www.freepdf-books.com




318

 

P a r t   I :  



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

  public void SetStart(int x) {

    start = x;

    val = start;

  }

}

class SeriesDemo2 {



  static void Main() {

    ByTwos twoOb = new ByTwos();

    Primes primeOb = new Primes();

    ISeries ob;

    for(int i=0; i < 5; i++) {

      ob = twoOb;

      Console.WriteLine("Next ByTwos value is " +

                          ob.GetNext());

      ob = primeOb;

      Console.WriteLine("Next prime number is " +

                          ob.GetNext());

    }


  }

}

The output from the program is shown here:



Next ByTwos value is 2

Next prime number is 3

Next ByTwos value is 4

Next prime number is 5

Next ByTwos value is 6

Next prime number is 7

Next ByTwos value is 8

Next prime number is 11

Next ByTwos value is 10

Next prime number is 13

In

Main( )

,

ob

 is declared to be a reference to an 

ISeries

 interface. This means that it can be 

used to store references to any object that implements 

ISeries

. In this case, it is used to refer 

to

twoOb

 and 


primeOb

, which are objects of type 



ByTwos

 and 


Primes

, respectively, which 

both implement 

ISeries

.

One other point: An interface reference variable has knowledge only of the methods 



declared by its 

interface

 declaration. Thus, an interface reference cannot be used to access 

any other variables or methods that might be supported by the object.

Interface Properties

Like methods, properties are specified in an interface without any body. Here is the general 

form of a property specification:

// interface property



type name

 {

   get;



   set;

}

www.freepdf-books.com




Download 4,07 Mb.

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