C# 0 The Complete Reference


Friend  adds a property called  IsWorkNumber



Download 4,07 Mb.
Pdf ko'rish
bet638/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   634   635   636   637   638   639   640   641   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Friend

 adds a property called 



IsWorkNumber

, which returns true if the 

telephone number is a work number.

To manage telephone lists, you create a class called 



PhoneList

. Because you want this 

class to manage any type of phone list, you make it generic. Furthermore, because part 

of the list management is looking up numbers given names, and vice versa, you add the 

constraint that requires that the type of objects stored in the list must be instances of a class 

derived from 



PhoneNumber

.

// PhoneList can manage any type of phone list



// as long as it is derived from PhoneNumber.

class PhoneList where T : PhoneNumber {

  T[] phList;

  int end;

  public PhoneList() {

    phList = new T[10];

www.freepdf-books.com



508

 

P a r t   I :  



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

    end = 0;

  }

  // Add an entry to the list.



  public bool Add(T newEntry) {

    if(end == 10) return false;

    phList[end] = newEntry;

    end++;

    return true;

  }


  // Given a name, find and return the phone info.

  public T FindByName(string name) {

    for(int i=0; i

      // Name can be used because it is a member of

      // PhoneNumber, which is the base class constraint.

      if(phList[i].Name == name)

        return phList[i];

    }


    // Name not in list.

    throw new NotFoundException();

  }

  // Given a number, find and return the phone info.



  public T FindByNumber(string number) {

    for(int i=0; i

      // Number can be used because it is also a member of

      // PhoneNumber, which is the base class constraint.

      if(phList[i].Number == number)

        return phList[i];

    }

    // Number not in list.



    throw new NotFoundException();

  }


  // ...

}

The base class constraint enables code inside 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   634   635   636   637   638   639   640   641   ...   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