samedi 1 juin 2019

How to override a constructor?

In my code I want to create a class called programmer which extends from the employee class.

The employee class constructor should take 4 parameters, and the programmer class constructor should take 5 parameters, 4 from the employee class and the other one from programmer class.

How can I do this?

class employee {

     private id: string;
     private name: string;
     private department: string;
     private salary: number;

    constructor(id: string , name: string , department: string , salary: number) {

        this.id = id;
        this.name = name;
        this.department = department;
        this.salary = salary;

    }



    speak() {
        console.log(`ID = ${this.id} , Name = ${this.name} , Department = ${this.department} , Salary = ${this.salary}`);
    }
}

class programmer extends employee {

    private programmingLang: string;

    constructor() {
        super();
    }

    speak() {
        super.speak() , console.log(` , Programming Language = ${this.programmingLang}`);
    }
}




Aucun commentaire:

Enregistrer un commentaire