eightminuseight icon

Class Extend Get Set

eightminuseight | PRO | 10/05/16 07:42:55 PM UTC | 0 ⭐ | 223 👁️ | Never ⏰ | []
JavaScript |

1 KB

|

None

|

0 👍

/

0 👎

class Person {
    protected name: string;
    constructor(name: string) { 
        this.name = name; 
    }
}
 
 
class Employee extends Person {
    
    public greeting: string;
    private department: string;
    private _age: number;
    public readonly numberOfLegs: number = 2;
 
    // getter for age
    get age(): number {
        return this._age;
    }
 
    // setter for age
    set age(newAge: number) {
        if (newAge > 10 && newAge < 100) {
            this._age = newAge;
        } else {
            console.log("Error: Age is out of Range");
        }
    }
 
    constructor(name: string, department: string) {
        super(name);
        this.department = department;
    }
 
    public getElevatorPitch() {
        return `Hello, my name is ${this.name} and I work in ${this.department}.`;
    }
}
 
let howard = new Employee("Howard", "Sales");
howard.age = 98;
console.log(howard.age);
console.log(howard.getElevatorPitch());
console.log(howard.name); // error

Comments