CSharpda Abstract classlardan foydalanish
Abstract class – bu class ob’yektini yaratib bo’lmaydigan class turi, uni base class sifatida ishlatish mumkin. Abstract classda abstract yoki abstract bo’lmagan metodlar va memberlardan foydalanish mumkin. Abstract classda maydon(field)ni abstract qilib yaratish MUMKIN EMAS, property(get;set;) va metodlarni abstract qilib yaratish MUMKIN.
Masalan:
Question 1: Abstract classda aniqlangan abstract metod(yoky property) va abstract bo’lmagan metodning(yoki property) qanday farqi bor?
Answer: abstract classdan inheritance olgan classda abstract classdagi abstract metodlar(propertylar) override qilinib qayta aniqlanishi shart. Abstract metod(yoki property) lar Abstract classda faqatgina e’lon qilinadi uning nima ich bajarishi esa Abstract classdan inheritance olgan classda aniqlanadi.
Masalan:
1) Methods
2) properties
Question 2: Abstract classning interfacedan farqi?
Answer: Abstract sinf abstract memberlardan va abstract bo’lmagan memberlardan iborat bo’lishi mumkin, interfaceda esa barcha memberlar abstract bo’lib, har bir child sinfda override qilinishi shart! Ya’ni interfaceda abstract bo’lmagan member yaratib bo’lmaydi!!!
Abstract sinfda abstract memberlarni yaratish interfacedagi kabi bir xil effect beradi
Masalan:
public abstract class MyClass
{
public abstract string Name{get;set;} //Mumkin
public abstract string ColumnName; //Mumkin emas, chunki bu FIELD
public abstract void GetValue() //Mumkin
{
//…
}
}
Question 1: Abstract classda aniqlangan abstract metod(yoky property) va abstract bo’lmagan metodning(yoki property) qanday farqi bor?
Answer: abstract classdan inheritance olgan classda abstract classdagi abstract metodlar(propertylar) override qilinib qayta aniqlanishi shart. Abstract metod(yoki property) lar Abstract classda faqatgina e’lon qilinadi uning nima ich bajarishi esa Abstract classdan inheritance olgan classda aniqlanadi.
Masalan:
1) Methods
public abstract class A
{
public string Name;
public abstract string GetName1();//Bu metodni GetName1(){…} kabi aniqlash mumkin emas
public string GetName2()
{
return “This is GetName2 method’s result”
}
}
public class B:A
{
//Bu yerda A classdagi GetName1 metodi override qlinib qayta aniqlanishi shart chunki u abstract metod, aks holda //xatolik yuz beradi.
public override string GetName1()
{
return “This is GetName1() method’s result”;
}
}
2) properties
abstract class A
{
protected int myNumber;
public abstract int numbers
{
get;
set;
}
}
class B: A
{
// abstract propertylarni Implement qilamiz
public override int numbers
{
get
{
return myNumber;
}
set
{
myNumber = value;
}
}
}
Question 2: Abstract classning interfacedan farqi?
Answer: Abstract sinf abstract memberlardan va abstract bo’lmagan memberlardan iborat bo’lishi mumkin, interfaceda esa barcha memberlar abstract bo’lib, har bir child sinfda override qilinishi shart! Ya’ni interfaceda abstract bo’lmagan member yaratib bo’lmaydi!!!
interface iSampleInterface
{
//Betta barcha memberlar avtomatik abstract yaratilvotti
int AddNumbers(int Num1, int Num2);
int MultiplyNumbers(int Num1, int Num2);
}
Abstract sinfda abstract memberlarni yaratish interfacedagi kabi bir xil effect beradi
Нет комментариев