Interfaces को Implement करना
In this page:
class ClassName implements InterfaceName {
property: type = value;
}
Basic Implementation
एक class declaration में implements InterfaceName जोड़ना require करता है कि वह class interface defines करने वाली हर property और method को satisfy करे।
अगर class में interface को चाहिए कुछ missing है, या इसके लिए गलत type है, TypeScript एक compile error report करता है।
उदाहरण: Basic Implementation
interface Greetable {
name: string;
}
class Person implements Greetable {
name: string;
constructor(name: string) {
this.name = name;
}
}
console.log(new Person("Ana").name);
Implementing Methods
एक interface implement करने वाले class को हर required method के लिए एक compatible implementation provide करना चाहिए, interface के declared parameter और return types को match करते हुए।
Class की अपनी implementation ज़्यादा specific हो सकती है, पर कभी interface के promise से कम capable नहीं।
उदाहरण: Implementing Methods
interface Speaker {
speak(): string;
}
class Robot implements Speaker {
speak(): string {
return "Beep boop";
}
}
console.log(new Robot().speak());
Implementing Multiple Interfaces
एक single class एक साथ कई interfaces implement कर सकता है, simply उन्हें implements के बाद comma-separated list करके। यह एक class को एक साथ कई independent contracts satisfy करने देता है, जैसे एक साथ Comparable और Serializable होना।
उदाहरण: Implementing Multiple Interfaces
interface Flyable {
fly(): string;
}
interface Swimmable {
swim(): string;
}
class Duck implements Flyable, Swimmable {
fly() { return "Flying"; }
swim() { return "Swimming"; }
}
const duck = new Duck();
console.log(duck.fly(), duck.swim());
Using Implemented Classes
एक given interface implement करने वाले किसी भी class का instance उस interface type की उम्मीद वाली किसी भी जगह उपयोग किया जा सकता है, class के अपने concrete name की परवाह किए बिना।
यही है जो interfaces को code लिखने के लिए उपयोगी बनाता है जो एक specific implementation के बजाय एक shape पर depend करता है।
उदाहरण: Using Implemented Classes
interface Speaker {
speak(): string;
}
class Robot implements Speaker {
speak() { return "Beep"; }
}
function announce(speaker: Speaker) {
console.log(speaker.speak());
}
announce(new Robot());
Implementing Extended Interfaces
जब एक class ऐसा interface implement करता है जो खुद दूसरे interfaces को extend करता है, class को उस interface द्वारा directly जोड़े गए requirements और इसके parents से inherited everything, दोनों को satisfy करना चाहिए।
TypeScript पूरे combined contract को check करता है, सिर्फ interface के अपने declared members को नहीं।
उदाहरण: Implementing Extended Interfaces
interface Named {
name: string;
}
interface Greetable extends Named {
greet(): string;
}
class Person implements Greetable {
constructor(public name: string) {}
greet() { return `Hi, I'm ${this.name}`; }
}
console.log(new Person("Zoe").greet());
- एक class पर
implements Speakerलिखना परspeak()method न जोड़ना, जो एक compile error है। implementsकोextendsके साथ confuse करना और class को interface से code inherit करने की उम्मीद करना।- एक method को एक अलग signature के साथ implement करना, जैसे
numberreturn करना जब interface कहता हैstring।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: