C# 0 The Complete Reference


ByTwos  and  Primes



Download 4,07 Mb.
Pdf ko'rish
bet412/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   408   409   410   411   412   413   414   415   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

ByTwos

 and 


Primes

 generate completely unrelated 

series of numbers, both implement 

ISeries

. As explained, an interface says nothing about 

the implementation, so each class is free to implement the interface as it sees fit.

Using Interface References

You might be somewhat surprised to learn that you can declare a reference variable of an 

interface type. In other words, you can create an interface reference variable. Such a variable 

can refer to any object that implements its interface. When you call a method on an object 

through an interface reference, it is the version of the method implemented by the object 

that is executed. This process is similar to using a base class reference to access a derived 

class object, as described in Chapter 11.

The following example illustrates the use of an interface reference. It uses the same 

interface reference variable to call methods on objects of both 

ByTwos

 and 


Primes

. For 


clarity, it shows all pieces of the program, assembled into a single file.

// Demonstrate interface references.

using System;

// Define the interface.

public interface ISeries {

  int GetNext(); // return next number in series

  void Reset(); // restart

  void SetStart(int x); // set starting value

}

// Use ISeries to implement a series in which each



// value is two greater than the previous one.

class ByTwos : ISeries {

  int start;

  int val;

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   408   409   410   411   412   413   414   415   ...   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