Classes में Interfaces Implement करना
In this page:
class ClassName implements Interface1, Interface2 {
// implement all members
}
Basic Interface Implementation
implements keyword एक class को एक interface से connect करता है, और compiler class को interface द्वारा declared हर property और method provide करने की ज़रूरत पड़ता है, या यह compile नहीं होगा।
उदाहरण: Basic Interface Implementation
interface Printable {
print(): void;
}
class Report implements Printable {
print() { console.log("Printing report"); }
}
new Report().print();
Interface Properties
एक interface उन properties को describe कर सकता है जो implementing classes को contain करनी चाहिए, TypeScript को class के against check करने के लिए एक structural contract देते हुए भले ही interfaces खुद कोई runtime code produce न करें।
उदाहरण: Interface Properties
interface Vehicle {
wheels: number;
}
class Car implements Vehicle {
wheels: number = 4;
}
console.log(new Car().wheels);
Interface Methods
Interfaces method signatures define कर सकते हैं जो implementing classes को provide करने ज़रूरी हैं, parameter और return types specify करते हुए बिना dictate किए कि method की body actually कैसे लिखी जाए।
उदाहरण: Interface Methods
interface Calculator {
add(a: number, b: number): number;
}
class BasicCalculator implements Calculator {
add(a: number, b: number): number {
return a + b;
}
}
console.log(new BasicCalculator().add(2, 3));
Multiple Interfaces Implement करना
एक class एक साथ एक से ज़्यादा interface implement कर सकती है, इसे कई independent contracts simultaneously satisfy करने देते हुए — उपयोगी जब एक class को, मान लीजिए, Comparable और Serializable दोनों के रूप में treat किया जाना ज़रूरी हो।
उदाहरण: Implementing Multiple Interfaces
interface Comparable {
compareTo(other: number): number;
}
interface Serializable {
serialize(): string;
}
class Score implements Comparable, Serializable {
constructor(public value: number) {}
compareTo(other: number) { return this.value - other; }
serialize() { return String(this.value); }
}
const s = new Score(90);
console.log(s.compareTo(80), s.serialize());
Interfaces Implement क्यों करें
Interfaces class contracts को explicit बनाते हैं और अलग-अलग, unrelated classes को same structural shape follow करने देते हैं, जो generic code को उनमें से किसी के साथ interchangeably काम करने की permission देता है।
उदाहरण: Why Implement Interfaces
interface Shape {
area(): number;
}
class Square implements Shape {
constructor(public side: number) {}
area() { return this.side ** 2; }
}
class Circle implements Shape {
constructor(public radius: number) {}
area() { return 3.14 * this.radius ** 2; }
}
function printArea(shape: Shape) {
console.log(shape.area());
}
printArea(new Square(3));
- यह उम्मीद करना कि
implementscode inherit करेगा, जब यह सिर्फ shape check करता है, इसलिए class को अब भी हर member खुद लिखना चाहिए। - interface का एक member miss करना, जैसे
print()छोड़ देना, जो class पर एक compile error देता है। - यह मान लेना कि एक interface runtime पर दिखता है, इसलिए
obj instanceof Printablefail होता है क्योंकि interfaces erased हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: