Last active
March 22, 2020 22:50
-
-
Save PranavBhatia/564a09cf0431846886ef32124b2ae48e to your computer and use it in GitHub Desktop.
We might have some extra code for defining types but in the end, it's only for the better.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Employee class is bascially a blueprint of any employee object that can be instantiated with this piece of code. | |
class Employee { | |
public employeeName: string; // A public variable of type STRING | |
private salary: number; // A private variable of type NUMBER | |
constructor(employeeName: string, salary: number) { | |
this.employeeName = employeeName; | |
this.salary = salary; | |
} // A contructor to create an employee object with a predefined salary and name. | |
public promotion(): void { | |
this.salary += 1000; | |
} // Adding 1000 to the emplyee's current salary. | |
public printSalary():void { | |
console.log(this.employeeName + '\'s salary is: ' + this.salary); | |
} // This method prints the employee name and his/her salary. | |
} | |
// Instantiate (create) an object from a class | |
let employee = new Employee('Bob', 1000); | |
employee.printSalary(); | |
employee.promotion(); | |
employee.printSalary(); | |
// Result => | |
// Bob's salary is: 1000 | |
// Bob's salary is: 2000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment