TypeScript Class 1
TypeScript Class 1
Ex:
class Demo
{
static s=0;
n=0;
constructor(){
Demo.s = Demo.s + 1;
this.n = this.n + 1;
}
Print(){
console.log(`s=${Demo.s} n=${this.n}`);
}
}
let obj1 = new Demo;
obj1.Print();
let obj2 = new Demo;
obj2.Print();
let obj3 = new Demo;
obj3.Print();
Access Modifiers
TypeScript class members can be defined by using 3 access modifiers
public
private
protected
Public member is accessible from any location.
Private member is accessible only within the class.
Protected member is accessible within the class.
Protected member is accessible only in derived class by using derived class object.
Print(){
console.log(`Name=${this.Name}\nPrice=${this.Price}\nStock=${this.Stock}`);
}
}
class Derived extends Product
{
Print(){
let obj = new Derived();
obj.Name;
obj.Stock;
}
}