C# 0 The Complete Reference


PART I C h a p t e r   1 8



Download 4,07 Mb.
Pdf ko'rish
bet643/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   639   640   641   642   643   644   645   646   ...   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  

515


PART IPART I

    // Number not in list.

    throw new NotFoundException();

  }


  // ...

}

// Demonstrate interface constraints.



class UseInterfaceConstraint {

  static void Main() {

    // The following code is OK because Friend

    // implements IPhoneNumber.

    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

    // implements IPhoneNumber.

    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"));

    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 implement IPhoneNumber.

//    PhoneList plist3 =

//        new PhoneList(); // Error!

  }


}

www.freepdf-books.com




516

 

P a r t   I :  



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

In this version of the program, the interface constraint specified by 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   639   640   641   642   643   644   645   646   ...   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