C# 0 The Complete Reference


PART I C h a p t e r   1 8



Download 4,07 Mb.
Pdf ko'rish
bet671/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   667   668   669   670   671   672   673   674   ...   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  

539


PART IPART I

  }


}

// A derived class of Gen that defines a second

// type parameter, called V.

class Gen2 : Gen {

  V ob2;

  public Gen2(T o, V o2) : base(o) {

    ob2 = o2;

  }


  public V GetObj2() {

    return ob2;

  }

}

// Create an object of type Gen2.



class GenHierDemo2 {

  static void Main() {

    // Create a Gen2 object for string and int.

    Gen2 x =

      new Gen2("Value is: ", 99);

    Console.Write(x.GetOb());

    Console.WriteLine(x.GetObj2());

  }


}

Notice the declaration of this version of 



Gen2

, which is shown here:

class Gen2 : Gen {

Here, 


T

 is the type passed to 



Gen

, and 


V

 is the type that is specific to 



Gen2

.

V

 is used to 

declare an object called 



ob2

 and as a return type for the method 



GetObj2( )

. In 


Main( )

, a 


Gen2

 object is created in which type parameter 



T

 is 


string

, and type parameter 



V

 is 


int

.

The program displays the following, expected, result:



Value is: 99

A Generic Derived Class

It is perfectly acceptable for a non-generic class to be the base class of a generic derived 

class. For example, consider this program:

// A non-generic class can be the base class of a generic derived class.

using System;

// A non-generic class.

class NonGen {

  int num;

  public NonGen(int i) {

www.freepdf-books.com




540

 

P a r t   I :  



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

    num = i;

  }

  public int GetNum() {



    return num;

  }


}

// A generic derived class.

class Gen : NonGen {

  T ob;


  public Gen(T o, int i) : base (i) {

    ob = o;

  }

  // Return ob.



  public T GetOb() {

    return ob;

  }

}

// Create a Gen object.



class HierDemo3 {

  static void Main() {

    // Create a Gen object for string.

    Gen w = new Gen("Hello", 47);

    Console.Write(w.GetOb() + " ");

    Console.WriteLine(w.GetNum());

  }

}

The output from the program is shown here:



Hello 47

In the program, notice how 



Gen

 inherits 



NonGen

 in the following declaration:

class Gen : NonGen {

Because


NonGen

 is not generic, no type argument is specified. Thus, even though 



Gen

declares the type parameter 



T

, it is not needed by (nor can it be used by) 



NonGen

. Thus, 


NonGen

 is inherited by 



Gen

 in the normal way. No special conditions apply.



Overriding Virtual Methods in a Generic Class

A virtual method in a generic class can be overridden just like any other method. For 

example, consider this program in which the virtual method 

GetOb( )

 is overridden:

// Overriding a virtual method in a generic class.

using System;

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   667   668   669   670   671   672   673   674   ...   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