Abstract class — mavhum sinflar va metodlar
Shakllar: doira, to'rtburchak, uchburchak — barchasida maydoni() metodi bo'lishi kerak, lekin hisoblash usuli har birida boshqacha. Shakl classini to'g'ridan instans sifatida yaratishning ma'nosi yo'q — faqat meros uchun asos.
Abstract class — to'g'ridan instans yaratib bo'lmaydigan, faqat meros uchun class.
abstract class va abstract metod
abstract class Shakl {
abstract nomi(): string; // abstract — implementatsiyasi yo'q
abstract maydoni(): number; // vorislar majburiy amalga oshiradi
tavsif(): string { // oddiy metod — vorislarga meros
return `${this.nomi()}: ${this.maydoni().toFixed(2)} m²`;
}
}
abstract metodida body yo'q — voris o'zi yozadi.
Vorislar
class Doira extends Shakl {
constructor(private radius: number) { super(); }
nomi(): string { return "Doira"; }
maydoni(): number { return Math.PI * this.radius ** 2; }
}
class To_rtburchak extends Shakl {
constructor(private en: number, private bo: number) { super(); }
nomi(): string { return "To'rtburchak"; }
maydoni(): number { return this.en * this.bo; }
}
const shakllar: Shakl[] = [
new Doira(5),
new To_rtburchak(4, 6),
];
shakllar.forEach(s => console.log(s.tavsif()));
// Doira: 78.54 m²
// To'rtburchak: 24.00 m²
new Shakl(); // ❌ — Cannot create an instance of an abstract class
Abstract class vs Interface
Interface — faqat tur, runtime da yo'q
Abstract class — tur + implementatsiya (oddiy metodlar), runtime da bor
interface Hayvon { ovoz(): string; } // faqat shartnoma
abstract class Hayvon { abstract ovoz(): string; // shartnoma
uxla() { console.log("Zzz..."); } // + umumiy implementatsiya
}
Abstract class — umumiy mantiq (oddiy metodlar) + majburiy metodlar birgalikda.
Template Method pattern
Abstract class ning kuchli qo'llanishi — jarayon skeleti, tafsilotlar vorislarga:
abstract class Hisobot {
// Shablon metod — tartib belgilangan
yaratish(): string {
const sarlavha = this.sarlavhaOl();
const mazmun = this.mazmunYig();
const xulosa = this.xulosaBer();
return `${sarlavha}\n${mazmun}\n${xulosa}`;
}
// Vorislар o'zi yozadi
protected abstract sarlavhaOl(): string;
protected abstract mazmunYig(): string;
protected abstract xulosaBer(): string;
}
class YillikHisobot extends Hisobot {
protected sarlavhaOl() { return "2024 YILLIK HISOBOT"; }
protected mazmunYig() { return "Daromad: 100M so'm"; }
protected xulosaBer() { return "Muvaffaqiyatli yil"; }
}
Tez-tez uchraydigan xatolar
Abstract classni instans qilish:
abstract class Shakl { abstract maydoni(): number; }
new Shakl(); // ❌
Abstract metodga body berish:
abstract class A {
abstract fn(): void { console.log("..."); } // ❌ — body bo'lmaydi
}
Xulosa
abstract class Asos {
abstract majburiy(): string; // voris yozadi
umumiy(): string { return "umumiy"; } // meros oladi
// new Asos() — ❌ to'g'ridan yaratib bo'lmaydi
}
class Voris extends Asos {
majburiy(): string { return "voris"; } // ✅ majburiy
}
// Interface vs Abstract:
// Interface — faqat shartnoma, runtime da yo'q
// Abstract — shartnoma + umumiy mantiq, runtime da bor
Keyingi dars
44-dars: Interface implementation — implements, bir nechta interface