C# 0 The Complete Reference


PhoneList  to access the properties  Name



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

PhoneList

 to access the properties 



Name

 and 


Number

 for any type of telephone list. It also guarantees that only valid types are used to 

construct a 

PhoneList

 object. Notice that 



PhoneList

 throws a 



NotFoundException

 if a name 

or number is not found. This is a custom exception that is declared as shown here:

class NotFoundException : Exception {

  /* Implement all of the Exception constructors. Notice that

     the constructors simply execute the base class constructor.

     Because NotFoundException adds nothing to Exception,

     there is no need for any further actions. */

www.freepdf-books.com



PART I

C h a p t e r   1 8 :  

G e n e r i c s  

509


PART IPART I

  public NotFoundException() : base() { }

  public NotFoundException(string str) : base(str) { }

  public NotFoundException(string str, Exception inner) :

    base(str, inner) { }

  protected NotFoundException(

    System.Runtime.Serialization.SerializationInfo si,

    System.Runtime.Serialization.StreamingContext sc) :

      base(si, sc) { }

}

Although only the default constructor is used by this example, 



NotFoundException

implements all of the constructors defined by 



Exception

 for the sake of illustration. 

Notice that these constructors simply invoke the equivalent base class constructor 

defined by 



Exception

. Because 



NotFoundException

 adds nothing to 



Exception

, there 


is no reason for any further action.

The following program puts together all the pieces and demonstrates 



PhoneList

. Notice 

that a class called 

EmailFriend

 is also created. This class does not inherit 



PhoneNumber

.

Thus, it 



cannot

 be used to create a 



PhoneList

.

// A more practical demonstration of a base class constraint.



using System;

// A custom exception that is thrown if a name or number is not found.

class NotFoundException : Exception {

  /* Implement all of the Exception constructors. Notice that

     the constructors simply execute the base class constructor.

     Because NotFoundException adds nothing to Exception,

     there is no need for any further actions. */

  public NotFoundException() : base() { }

  public NotFoundException(string str) : base(str) { }

  public NotFoundException(string str, Exception inner) :

    base(str, inner) { }

  protected NotFoundException(

    System.Runtime.Serialization.SerializationInfo si,

    System.Runtime.Serialization.StreamingContext sc) :

      base(si, sc) { }

}

// A base class that stores a name and phone number.



class PhoneNumber {

  public PhoneNumber(string n, string num) {

    Name = n;

    Number = num;

  }

  public string Number { get; set; }



  public string Name { get; set; }

}

// A class of phone numbers for friends.



class Friend : PhoneNumber {

www.freepdf-books.com




510

 

P a r t   I :  



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

  public Friend(string n, string num, bool wk) :

    base(n, num)

  {


    IsWorkNumber = wk;

  }


  public bool IsWorkNumber { get; private set; }

  // ...


}

// A class of phone numbers for suppliers.

class Supplier : PhoneNumber {

  public Supplier(string n, string num) :

    base(n, num) { }

  // ...


}

// Notice that this class does not inherit PhoneNumber.

class EmailFriend {

  // ...


}

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

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

    }

www.freepdf-books.com




Download 4,07 Mb.

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