C# 0 The Complete Reference


PART I C h a p t e r   1 2



Download 4,07 Mb.
Pdf ko'rish
bet409/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   405   406   407   408   409   410   411   412   ...   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  

313


PART IPART I

The methods that implement an interface must be declared 



public

. The reason for this is 

that methods are implicitly public within an interface, so their implementation must also be 

public. Also, the return type and signature of the implementing method must match exactly 

the return type and signature specified in the 

interface

 definition.

Here is an example that implements the 

ISeries

 interface shown earlier. It creates a class 

called

ByTwos

, which generates a series of numbers, each two greater than the previous one.

// Implement ISeries.

class ByTwos : ISeries {

  int start;

  int val;

  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;

  }

}

As you can see, 



ByTwos

 implements all three methods defined by 



ISeries

. As explained, 

this is necessary since a class cannot create a partial implementation of an interface.

Here is a class that demonstrates 



ByTwos

:

// Demonstrate the ByTwos interface.



using System;

class SeriesDemo {

  static void Main() {

    ByTwos ob = new ByTwos();

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

      Console.WriteLine("Next value is " +

                         ob.GetNext());

    Console.WriteLine("\nResetting");

    ob.Reset();

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

      Console.WriteLine("Next value is " +

                         ob.GetNext());

www.freepdf-books.com



314

 

P a r t   I :  



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

    Console.WriteLine("\nStarting at 100");

    ob.SetStart(100);

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

      Console.WriteLine("Next value is " +

                         ob.GetNext());

  }

}

To compile 



SeriesDemo

, you must include the files that contain 




Download 4,07 Mb.

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