C# 0 The Complete Reference


PART I C h a p t e r   1 8



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


PART I

C h a p t e r   1 8 :  

G e n e r i c s  

511


PART IPART 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();

  }

  // ...


}

// Demonstrate base class constraints.

class UseBaseClassConstraint {

  static void Main() {

    // The following code is OK because Friend

    // inherits PhoneNumber.

    PhoneList plist = new PhoneList();

    plist.Add(new Friend("Tom", "555-1234", true));

    plist.Add(new Friend("Gary", "555-6756", true));

    plist.Add(new Friend("Matt", "555-9254", false));

    try {

      // Find the number of a friend given a name.

      Friend frnd = plist.FindByName("Gary");

      Console.Write(frnd.Name + ": " + frnd.Number);

      if(frnd.IsWorkNumber)

        Console.WriteLine(" (work)");

      else

        Console.WriteLine();

    } catch(NotFoundException) {

      Console.WriteLine("Not Found");

    }

    Console.WriteLine();



    // The following code is also OK because Supplier

    // inherits PhoneNumber.

    PhoneList plist2 = new PhoneList();

    plist2.Add(new Supplier("Global Hardware", "555-8834"));

    plist2.Add(new Supplier("Computer Warehouse", "555-9256"));

    plist2.Add(new Supplier("NetworkCity", "555-2564"));

www.freepdf-books.com



512

 

P a r t   I :  



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

    try {

      // Find the name of a supplier given a number.

      Supplier sp = plist2.FindByNumber("555-2564");

      Console.WriteLine(sp.Name + ": " + sp.Number);

    } catch(NotFoundException) {

        Console.WriteLine("Not Found");

    }


    // The following declaration is invalid because EmailFriend

    // does NOT inherit PhoneNumber.

//    PhoneList plist3 =

//        new PhoneList(); // Error!

  }

}

The output from the program is shown here:



Gary: 555-6756 (work)

NetworkCity: 555-2564

You might want to try experimenting with this program a bit. For example, try creating 

different types of telephone lists. Also, try using 




Download 4,07 Mb.

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