Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

class-------->variables and methods

variable---->store the value


method ---->perform operation
constructor---->initialization,implementing Dependecny injection
object creation-------> Memory allocation
Reference variable ------>Accessability
this-----------------------> Default Reference variable which is used to access
instance variables
user defined reference variable-------->Access instance methods
=================================================
constructor:-constructor is a member of a class which is used to initialize the
values for instance variables
in Angular we can implement Dependency Injection by using constructor
Constructor

Default constructor Parameterised Constructor


Default constructor:- it is used to initialize default values for instance
variables
Rules to be followed while declaring constructor:-
1. constructor must declare with constructor keyword
2. constructor will gets invoked immediately at the time of creating an object for
a class
3. constructor doesnot have return type
===========================================================================
class A
{
x:number;
y:number;
constructor()
{
this.x=10;
this.y=5;
}
}
var a1:A=new A();
==========================================
new will create object i.e memory will allocate for instance variables
A() will invoke constructor
Reference variable (a1) was assigned to the object
==========================================
Parameterised constructor:- it is used to initialize userdefined values instead of
default values
Rules to be followed while declaring parameterised constructor:-
1. constructor must declare with constructor keyword
2. At the time of declaring the constructor we have to declare parameters and the
time of creating object we need to pass values
3. The no of values,order of values,type of values that we pass must match with no
of ,order of
type of parameters
========================================
class Student
{
sno:number;
sname:string;
constructor(no:number,name:string)
{
this.sno=no;
this.sname=name;
}
GetStudent()
{
console.log(this.sno+" "+this.sname);
}
}
var s1:Student=new Student(101,"anil");
s1.GetStudent();
var s2:Student=new Student(102,"sunil");
s2.GetStudent();
==============================
class MyService
{

class AppComponent class EmpComponent


{ {
constructor(ms:MyService) constructor(ms:MyService)
{ {

} }
} }
=====================================================================
note:- in the above program Angular Framework will create object for service class
and
it will inject the object within the components through Dependency injection
Mechanism
with the help of that reference variable ms we can access MyService class
variabless,methods
===========================================================================
1. create a Folder in F Drive with name Demo
2. open visualstudio code-------->New file
3.
class Employee
{
eno:number;
ename:string;
salary:number;
da:number;
hra:number;
tsal:number;
constructor(no:number,name:string,sal:number)
{
this.eno=no;
this.ename=name;
this.salary=sal;
}
CalDa()
{
this.da=0.2*this.salary;
}
CalHra()
{
this.hra=0.4*this.salary;
}
CalTsal()
{
this.tsal=this.salary+this.da+this.hra;
}
DispEmpData()
{
console.log("Eno is "+this.eno);
console.log("Ename is "+this.ename);
console.log("Basic Salary is "+this.salary);
console.log("Da is "+this.da);
console.log("Hra is "+this.hra);
console.log("Tsal is "+this.tsal);
}
}
var e1:Employee=new Employee(101,"anil",20000);
e1.CalDa(); //calling the method means the logic that was writtejn inside
e1.CalHra(); the method will gets executed
e1.CalTsal();
e1.DispEmpData();
4. save the program in D:/Demo with name emp.ts
go to terminal---->New terminal---->
F:
cd Demo
5. compile the program
tsc emp.ts
6. run the program
node emp

You might also like