In Typescript, Classes provide a way to create object-oriented structures, encapsulating both data(variables) and behaviour(methods/functions) into a single unit.
Classes are a core feature of the language, and they offer a more organized and reusable approach to writing code.
We can declare a class using the "class" keyword.
class Employee { // Properties name: string; deptNo: number; deptName: string; // Constructor constructor(name: string, deptNo: number, deptName: string) { this.name = name; this.deptNo = deptNo; this.deptName = deptName; } // Method getEmployee(): string { return { name: this.name, deptNo: this.deptNo, deptName: this.deptName, }; } }
`Employee` is a class with properties ("name", "deptNo" and "deptName"), a constructor to initialize those properties, and a method ("getEmployee") to perform an action.
class Employee { // Properties name: string; deptNo: number; deptName: string; // Constructor constructor(name: string, deptNo: number, deptName: string) { this.name = name; this.deptNo = deptNo; this.deptName = deptName; } // Method getEmployee(): string { return { name: this.name, deptNo: this.deptNo, deptName: this.deptName, }; } } let employee = new Employee("Alice", 1007, 'HR'); console.log(employee.getEmployee()); // Output: { name: "Alice", deptNo: 1007, deptName: "HR"}
We can create an instance of a class using the "new" keyword.
class Department { private _deptNo: number; get deptNo(): number { console.log("Getter Function called", this._deptNo) return this._deptNo; } set deptNo(value: number) { console.log("Setter Function called", value) if (value > 0) { this._deptNo = value; } else { console.error("deptNo must be greater than 0."); } } } const dept = new Department(); dept.deptNo = 1007; console.log(dept.deptNo)
We can use getters and setters to control access to class properties.
The setter methods modify the property’s value. A setter function is also known as a mutator. while The getter methods return the value of the property’s value. A getter function is also called an accessor.
When we assign value to `dept.deptNo = 1007`, the `deptNo` setter method is invoked.
class CircleOperation { static PI: number = 3.14159; static calculatePerimeter(radius: number): number { return 2 * this.PI * radius; } static calculateArea(radius: number): number { return this.PI * radius * radius; } }
After that, we try to access the value of `dept.deptNo`, it will invoke the `get method` of `deptNo`.
abstract class Circle { abstract calculateArea(): number; abstract calculatePerimeter(): number; }
We can use the "static" keyword to define static properties and methods that belong to the class rather than instances.
Note: PI is a static property, calculatePerimeter and calculateArea is a static method.
We can create abstract classes with abstract methods that must be implemented by derived classes.
Classes in TypeScript provide a powerful mechanism for structuring our code in an object-oriented way.
They support encapsulation, inheritance, polymorphism, and other essential concepts of object-oriented programming.
Understanding and effectively using classes is key to building maintainable and scalable TypeScript applications.