Virtual usul deb baza sinfida virtual so’zi bilan e’lon kilingan va shu bilan birga bitta yoki bir nechta xosila sinflarda qaytadan anaqlangan usulga aytiladi.
Agarda usulni baza sinfda virtual deb e’lon qilish uchun virtual so’zidan foydalansa, uni xosila sinfda qayta aniqlash uchun override so’zidan foydalanadi.
Usullarning dinamik nozimlash – bu qayta aniqlangan usulni dastur bajarilish vaqtida chaqirilishi.
G’G’ Demonstratsiya virtualnogo metoda.
using System;
class Base {
G’G’ Sozdaem virtualno’y metod v bazovom klasse.
public virtual void who ( ) {
Console.WriteLine ("Metod who ( ) v klasse Base.");
}
}
class Derivedl : Base {
G’G’ Pereopredelyaem metod who ( ) v proizvodnom klasse.
public override void who ( ) {
Console.WriteLine("Metod who ( ) v klasse Derivedl");
}
}
class Derived2 : Base {
G’G’ Snova pereopredelyaem metod who ( )
G’G’ v drugom proizvodnom klasse.
public override void who ( ) {
Console.WriteLine("Metod who ( ) v klasse Derived2");
}
}
class OverrideDemo {
public static void Main ( ) {
Base baseOb q new Base ();
Derivedl dOb1 - new Derived1 ( );
Derived2 dOb2 q new Derived2 ( );
Base baseRef; G’G’ Sso’lka na bazovo’y klass.
baseRef q baseOb;
baseRef.who ( );
baseRef q dObl;
baseRef.who ( ) ;
baseRef q dOb2;
baseRef.who ( );
}
}
Dastur bajarilgandan so’ng quyidagi natijaga erishamiz.
Metod who ( ) v klasse Base.
Metod who ( ) v klasse Derived1
Metod who ( ) v klasse Derived2
Virtual usulni qaytadan aniqlash shart emas. Agar xosila sinf virtual usulda o’z talqinini bermasa, baza sinfda aniqlangan talqindan foydalanadi.
G’* yesli virtualno’y metod ne pereopredelen
v proizvodnom klasse, ispolzuetsya metod
bazovogo klassa. *G’
using System;
class Base {
G’G’ Sozdaem virtualno’y metod v bazovom klasse.
public virtual void who(){
Console.WriteLine("Metod who() v klasse Base");
}
}
class Derivedl : Base {
G’G’ Pereopredelyaem metod who () v proizvodnom klasse.
public override void who () {
Console.WriteLine("Metod who() v klasse Derived1");
}
}
class Derived2 : Base {
G’G’ Etot klass ne pereopredelyaet metod who ( ).
}
class NoOverrideDemo {
public static void Main ( ) {
Base baseOb q new Base ( );
Derived1 dObl q new Derived1( );
Derived2 dOb2 q new Derived2 ( );
Base baseRef; G’G’ Sso’lka na bazovo’y klass.
baseRef q baseOb;
baseRef.who ( ) ;
baseRef q dObl;
baseRef.who ( ) ;
baseRef q dOb2;
baseRef.who ( ); G’G’ Vo’zo’vaet metod who ( ) klassa Base.
}
}
Dastur bajarilgandan so’ng quyidagi natijaga erishamiz.
Metod who ( ) v klasse Base
Metod who ( ) v klasse Derived1
Metod who ( ) v klasse Base
Agar yuqori darajali ierarxiyada xosila sinfi virtual usulda qaytadan aniqlanmasa, unda ierarxiyada eng pastda joylashgan virtual usuldan foydalanadi.
G’* yesli proizvodno’y klass ne pereopredelyaet virtualno’y
metod v sluchae mnogourovnevoy ierarxii, budet vo’polnen
pervo’y pereopredelenno’y metod, kotoro’y obnarujitsya
pri prosmotre ierarxicheskoy lestnitso’ v napravlenii
snizu vverx. *G’
using System;
class Base {
G’G’ Sozdaem virtualno’y metod v bazovom klasse.
public virtual void who ( ) {
Console.WriteLine("Metod who ( ) v klasse Base");
}
}
class Derivedl : Base {
G’G’ Pereopredelyaem metod who () v proizvodnom klasse,
public override void who() {
Console.WriteLine("Metod who() v klasse Derived1");
}
class Derived2 : Derived1 {
G’G’ Etot klass ne pereopredelyaet metod who().
}
class Derived3 : Derived2 {
G’G’ Etot klass takje ne pereopredelyaet metod who().
}
class NoOverrideDemo2 {
public static void Main() {
Derived3 dOb q new Derived3();
Base baseRef; G’G’ Sso’lka na bazovo’y klass.
baseRef q dOb;
baseRef.who ( ); G’G’ Vo’zo’vaet metod who ( )
G’G’ iz klassa Derived1.
}
}
Dastur bajarilgandan so’ng quyidagi natijaga erishamiz.
Metod who ( ) v klasse Derived1
Bu yerda Derived1 sinfini qoldirgan Derived2 sinfi Derived3 sinfini qoldiradi, Derived1 esa o’z navbatida Base sinfini qoldiradi. Bu programmani natijalari who() usuli Derived3 sinfida ham, Derived2 sinfida ham qayta aniqlanmagan, lekin Derived1 sinfida qayta aniqlangan.
Do'stlaringiz bilan baham: |