Here , We will gonna learn how we can implement Basics Object Oriented Programming concepts in Typescript.
Brief Introduction of Typescript
Prerequisite
Iโm assuming that you are familiar with Basic OOP Concepts and also you haveย basic knowledge about Typescript.
Now First we will gonna create index.ts file. In This we will gonna implement OOPS concepts.
Classes In Typescript
// Define a class class Student{ }
//instantiate a class let obj = new Student();
Class Properties
// Define class properties class Student{ public id:number; public name:string; public rollNo:number; }
// Create an object and use properties of that class let obj = new Student(); obj.id = 12; obj.name = 'Karan'; obj.rollNo = 47
// pass a private property in constructor and get that value with getter class Student{ public id:number; public name:string; public rollNo:number; // Default Parameter value constructor(private mark:number=50){ } get getMarks(){ return this.mark; } } // Pass value in constructor let obj = new Student(69); obj.id = 12; obj.name = 'Karan'; obj.rollNo = 47 // Get Marks with Getter console.log(obj.getMarks);//69
Methods In Class
// This method will print student details on the terminal getDetails(){ console.log(` Id : ${this.id} \n Name : ${this.name} \n Roll No: ${this.rollNo}`); }
Constructor of a class
// Created a class constructor and i passed a variable here , you can also create an empty constructor constructor(public mark?:number){ console.log('Student Invoked'); }
Interface In Typescript
// Define an interface interface IStudent { }
// Implement or inherit an interface class Student implements IStudent{ }
// interface with properties interface IStudent { id:number; name:string; rollNo:number; }
interface IStudent { id?:number; name?:string; rollNo?:number; }
// We can omit implemention of SomeFunc() method in derived class interface IStudent { id?:number; name?:string; rollNo?:number; someFunc?():any; }
Inheritance In Typescript
// Base Class class Marks { constructor(){ console.log('Marks Invoked'); } } // Derived class class Student extends Marks { constructor(){ super(); console.log('Student Invoked'); } }
interface IStudent { id?:number; name?:string; rollNo?:number; } class Student implements IStudent { constructor(){ } }
interface IMarks{ marks?:number; } interface IStudent { id?:number; name?:string; rollNo?:number; } class Student implements IStudent,IMarks { constructor(){ } }
Abstraction In Typescript
class Student{ public id:number; public name:string; public rollNo:number; // Default parameter value constructor(private marks:number = 50){ console.log('Student Invoked'); } get getMarks(){ return this._marks; } } let obj = new Student(69); obj.id = 12; obj.name = 'Karan'; obj.rollNo = 47 console.log(obj.getMarks);
Encapsulation In Typescript
// Account Block class Account { constructor(private accountNumber:number=1000000,private accountHolderName:string="Not Available",private accountBalance:number=0){ } get getAccountNumber():number{ return this.accountNumber; } get getAccountHolderName():string{ return this.accountHolderName; } get getAccountBalance():number{ return this.accountBalance; } } let acc = new Account(123165465,"Karan Rajgor",50000); console.log(`Account Number: ${acc.getAccountNumber} \n Account Holder Name: ${acc.getAccountHolderName} \n Account Balance: ${acc.getAccountBalance}`); //Account Number: 123165465 //Account Holder Name: Karan Rajgor //Account Balance: 50000
I hope this article will helps you to understand OOP concept of Typescript.
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular