//Example1=========================
// Class
class User {
private name: string;
private readonly city: string;
public country: string;
public constructor(name: string) {
this.name = name;
}
public getName(): string {
return this.name;
}
}
const user = new User('Jane');
console.log(user.getName());
user.name; // its not accessible from outside the class since its private
//Example 2========================
class Admin {
name: string = 'A';
age: number = 18;
skills: string[] = ['B', 'C', 'D', 'E', 'F'];
constructor(name: string, age: number, skills: string[]) {
this.name = name;
this.age = age;
this.skills = skills;
}
show(): string {
return `I am ${this.name},${this.age} years old, having skills in ${this.skills}`;
}
}
type Data = {
name: string;
age: number;
skills: string[];
roleNo: number;
subject: string;
};
class User extends Admin {
role_no: number;
subject: string;
constructor(
name: string,
age: number,
skills: string[],
roleNo: number,
subject: string
) {
super(name, age, skills);
this.role_no = roleNo;
this.subject = subject;
}
show(): string {
return `${super.show()} and roll number is : ${
this.role_no
} my faourite subjects : ${this.subject}`;
}
}
const admin = new Admin('Saten', 36, ['React', 'Web', 'Mobile', 'DevOps']);
const user = new User(
'Pratap',
33,
['Typescript', 'Web', 'Mobile', 'DevOps'],
450,
'Mathmetics'
);
console.log(obj.show());