Objects:
real time objects
we are doing online shopping product list
method to add to card etc...add product, delete product, edit product - this product works with rendering, storing date etc.
each piece is object having certain functionality
Objects are instances of the classes.
Classes:
Classes are blueprint to the objects.
Classes holds the objects and methods they have.
real time objects
we are doing online shopping product list
method to add to card etc...add product, delete product, edit product - this product works with rendering, storing date etc.
each piece is object having certain functionality
Objects are instances of the classes.
Classes:
Classes are blueprint to the objects.
Classes holds the objects and methods they have.
//First class
class Department {
name: string;
constructor(n: string){
this.name = n;
}
}
const accountName = new Department("Accounts"); // here Department is the blueprint to the object
console.log(accountName);
Constructor Functions and this key word
class Department {
name: string;
constructor(n: string){
this.name = n;
}
describe(this: Department){
console.log("This info is belongs to "+ this.name);
}
}
const accountName = new Department("Accounts");
accountName.describe();
private property can accessible only with in the method or class.
string array
public properties can accessible any whereprivate property can accessible only with in the method or class.
class Depo {
public name: string;
private employees: string [] = [];
constructor(n: string){
this.name = n;
}
addEmployee(employee: string){
this.employees.push(employee);
}
printEmployeeInfo(){
console.log(this.employees.length);
console.log(this.employees);
}
}
const busStn1 = new Depo("Account");
busStn1.addEmployee("Saga");
busStn1.addEmployee("Koti");
busStn1.printEmployeeInfo();
readonly
public readonly id;
if we try accessing the ID variable, we will get compilation error.
Prototype
Inheritance
The class that inherit the properties of the base class
Super is the method we have to use it in the constructor of the In inheritance class.
Super method always refers to the base class constructor and its parameters.
class Depo {
public name: string;
private employees: string [] = [];
constructor(private id: string,public n: string){
this.name = n;
}
describe(){
console.log(`Department (${this.id}): (${this.name})`)
}
addEmployee(employee: string){
this.employees.push(employee);
}
printEmployeeInfo(){
console.log(this.employees.length);
console.log(this.employees);
}
}
class ITDepartment extends Depo{
adminsArr: string[];
constructor(id: string, admins: string[]){
super(id, "IT");
this.adminsArr = admins;
}
printAdmins(){
console.log(this.adminsArr);
}
}
const busStn1 = new ITDepartment("1",["Saga"]);
busStn1.addEmployee("Saga");
busStn1.addEmployee("Teja");
busStn1.printEmployeeInfo();
busStn1.describe();
busStn1.printAdmins();
Overriding the properties
protected employee : string;
protected key word variable can be used with in the class and inherited class with respective to the base class.
and out side any other class, it won't be available.
Getter and Setter:
class Depo {
public name: string;
private employees: string [] = [];
constructor(private id: string,public n: string){
this.name = n;
}
describe(){
console.log(`Department ${this.id}: ${this.name}`)
}
addEmployee(employee: string){
this.employees.push(employee);
}
printEmployeeInfo(){
console.log(this.employees.length);
console.log(this.employees);
}
}
class ITDepartment extends Depo{
protected adminsArr: string[];
private ourReports : string;
get mostRecentReport(){
if(this.ourReports){
return this.ourReports
}
else{
throw new Error("No report found");
}
}
set mostRecentReport(value: string){
if(!value){
throw new Error("Please pass a value");
}
this.addReport(value);
}
constructor(id: string, admins: string[], ourReports: string){
super(id, "Saga");
this.adminsArr = admins;
this.ourReports = ourReports;
}
printAdmins(){
console.log(this.adminsArr);
}
addReport(text: string){
this.ourReports = text;
}
}
class SalesDepartment extends Depo{
}
const output1 = new ITDepartment("1",["Saga"],"teja_report");
// busStn1.addEmployee("Saga");
// busStn1.addEmployee("Teja");
// busStn1.printEmployeeInfo();
// output1.describe();
// output1.printAdmins();
output1.mostRecentReport = "Lokesh";
console.log(output1.mostRecentReport);
Interfaces
An interface describes the structure of an object.
interface Person{
name: string;
age: number;
greet(pharse:string):void
}
let user: Person;
user = {
"name": "Saga",
"age": 28,
greet(pharse: string){
console.log("Hello, Greet you "+pharse);
}
}
user.greet("Saga Koti");
example 2:
interface Person1{
name1: string;
//age: number;
greet1(pharse:string):void
}
class UserClass implements Person1{
name1: string;
age1 = 30;
constructor(n: string){
this.name1 = n;
console.log(this.name1);
}
greet1(pharse: string){
console.log("phrase:"+pharse+" , name:"+this.name1);
}
}
let myuser = new UserClass("Saga");
myuser.greet1("Hey!! how are you?");
readonly
In interfaces, we can't use private or static so we will use more often readonly before the key words
Extending Interfaces
interface Named{
name: string;
}
interface Greeted extends Named{
greetMe(pharse: string):void
}
class MyUsers implements Greeted{
name: string;
age= 30;
constructor(n: string){
this.name = n;
}
greetMe(pharse: string){
console.log("Helo "+this.name+"!! "+pharse);
}
}
let myuserIs = new MyUsers("Saga");
myuserIs.greetMe("How are you today!!");
Otherway to implement interface
interface AddFn{
(x: number, y: number): number
}
let addition: AddFn;
addition = (n1: number, n2: number) => {
return n1+n2;
}
console.log(addition(3,5));
Optional Parameters & Property:
interface OptionalCheck {
readonly internalName?:string;
outputName?: string
}
class Puple implements OptionalCheck{
internalName?: string;
age = 30;
constructor(n: string){
if(n!=""){
this.internalName = n;
}
}
greetMeToday(phrase: string){
if(this.internalName){
console.log(this.internalName)
}
else{
console.log("Hi "+phrase);
}
}
}
let gtMe = new Puple("Saga");
gtMe.greetMeToday("Bala");
Comments
Post a Comment