Gado da mu'amala sune mahimman ra'ayoyi guda biyu a cikin TypeScript
, kuma suna taka muhimmiyar rawa wajen haɓaka aikace-aikace. Anan akwai tattaunawa akan waɗannan ra'ayoyin da amfaninsu da fa'idodinsu wajen haɓaka aikace-aikacen:
Gado
Gado a cikin TypeScript
yana ba wa ƙaramin aji damar gadon kadarori da hanyoyin daga babban aji. Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙarƙashin Ƙaƙwalwa na iya haɓaka da haɓaka abubuwan da ke akwai na superclass.
Don amfani da gado, muna amfani da extends
kalmar maɓalli don bayyana cewa ƙaramin aji ya gaji daga babban aji.
Misali:
class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
eat() {
console.log(this.name + " is eating.");
}
}
class Dog extends Animal {
bark() {
console.log(this.name + " is barking.");
}
}
const dog = new Dog("Buddy");
dog.eat(); // Output: "Buddy is eating."
dog.bark(); // Output: "Buddy is barking."
A cikin misalin da ke sama, Dog
ajin ya gaji daga Animal
aji kuma ya tsawaita ta hanyar ƙara bark()
hanyar. Ajin Dog
na iya amfani da eat()
hanyar da aka gada daga Animal
ajin.
Hanyoyin sadarwa
Hanyoyin sadarwa a cikin TypeScript
ma'anar saitin kaddarori da hanyoyin da abu dole ne ya bi su. Suna ƙayyadad da kwangila don abubuwan da ke raba abubuwan gama gari.
Don amfani da musaya, muna amfani da interface
kalmar maɓalli don ayyana abin dubawa.
Misali:
interface Shape {
calculateArea(): number;
}
class Circle implements Shape {
radius: number;
constructor(radius: number) {
this.radius = radius;
}
calculateArea() {
return Math.PI * this.radius * this.radius;
}
}
const circle = new Circle(5);
console.log(circle.calculateArea()); // Output: 78.53981633974483
A cikin misalin da ke sama, Shape
mahaɗin yana bayyana calculateArea()
hanyar da kowane abu dole ne ya bi. Ajin Circle
yana aiwatar da Shape
haɗin gwiwa kuma yana ba da aiwatarwa don calculateArea()
hanyar.
Fa'idodin gado da mu'amala a cikin haɓaka aikace-aikace:
- Gado yana sauƙaƙe sake amfani da lambar kuma yana rage kwafi. Lokacin da ƙaramin aji ya gaji daga babban aji, zai iya sake amfani da kaddarorin da hanyoyin da aka riga aka aiwatar a cikin babban aji.
- Hanyoyin sadarwa suna bayyana kwangiloli kuma suna tilasta bin ƙayyadadden mu'amala, tabbatar da cewa abubuwa sun cika ka'idojin da ake buƙata. Suna kafa tsarin gama gari don haɓakawa da amfani da abubuwa.
- Dukansu gado da mu'amala suna ba da gudummawa ga sassauƙa a ƙira da haɓaka aikace-aikace, ba da damar ra'ayoyi kamar polymorphism da sake amfani da lambar.
A taƙaice, gado da mu'amala sune mahimman ra'ayoyi a cikin TypeScript
. Suna taka muhimmiyar rawa wajen haɓaka aikace-aikacen, haɓaka sake amfani da lambar, sassauƙa, da bin ƙayyadaddun kwangila.