Encapsulation is Create A Class With Related Variables And Functions
Like LocalStorage In JAVASCRIPT
You Have Variables Like:
length which is number
You Have Methods Like:
getItem
setItem
removeItem
clear
Encapsulation Concept reduce the complexity of the app and reduce the spaghetti Code
class kia extends Car {
constructor(name: string, createdAt: string, model: string) {
// Like constructor but from parent class This Super function have the same args of parent class
super(name, createdAt, model)
}
// Now In This Class We Have The Same Properties And Methods In Parent Class
// We Can Add More As We Want
greeting(companyName: string) {
console.log(`Hello ${companyName}`)
}
}
Polymorphism is the ability to create a class that has more than one form or in other words, classes have the same methods but different implementations
السؤال
منتصر احمد
لقد بدات تعلم oop (object orianted programming) اليوم منذ حوالي ساعه او ساعتين وقد كتبت هذا الملف عن الoop في typescript.
محتاج أراكم فيها ولو في اي تعديل.
يمكنك تحميل الملف من classes.txt
او يمكنك قرآئته من هنا
Why Should I Use Classes Insted Of Functions ?
Classes Offer You A Better Code With Features Like:
1- Encapsulation
2- Inheritance
3- Abstrution
4- Polymorphism
***************************************************
Encapsulation is Create A Class With Related Variables And Functions
Like LocalStorage In JAVASCRIPT
You Have Variables Like:
length which is number
You Have Methods Like:
getItem
setItem
removeItem
clear
Encapsulation Concept reduce the complexity of the app and reduce the spaghetti Code
***************************************************
Inheritance
Inheritance From Class Is Very Important Concept
The Main Idea Behind Concept Is Child Class inherit properties and methods from the parent class
Imagine We Have More Than On Car Like Tesla, Toyota, Honda, kia, etc....
if we create single class to all of these cars the code will be Agly, Bad and spaghetti
So We Use The Inheritance Concept
We Use It Like This:
// Parent Class
class Car {
constructor(public name: string, public createdAt: string, public model: string) {}
printCarProperties() {
console.log(`Name: ${name}, createdAt: ${createdAt}, model: ${model}`)
}
}
// Child Class
class kia extends Car {
constructor(name: string, createdAt: string, model: string) {
// Like constructor but from parent class This Super function have the same args of parent class
super(name, createdAt, model)
}
// Now In This Class We Have The Same Properties And Methods In Parent Class
// We Can Add More As We Want
greeting(companyName: string) {
console.log(`Hello ${companyName}`)
}
}
***************************************************
Abstrution
Abstrution is Important Concept To Understand
The Main Idea Behind Abstrution Is _Creating A Model For Variable Or Methods In Main Class_
Like:
abstract class MainClass {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
abstract printUserInfo():void;
abstract username: string;
abstract userPhoneNumber: string;
}
class Child extends MainClass {
constructor(name: string, age: number) {
super(name, age)
}
printUserInfo(): void {
console.log(this.name, this.age)
}
username: string = "Hello Username";
userPhoneNumber: string = "+20 100 892 18 10";
}
It's Hard To Understand In The Beginning But It will be very easy
***************************************************
Polymorphism
Polymorphism is the ability to create a class that has more than one form or in other words, classes have the same methods but different implementations
1 جواب على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.