-
Inheritance IIStudy/JavaScript 2020. 5. 8. 14:44
Inheritance II
In the last exercise, we created a parent class named Animal for two child classes named Cat and Dog.
The Animal class below contains the shared properties and methods of Cat and Dog.
class Animal { constructor(name) { this._name = name; this._behavior = 0; } get name() { return this._name; } get behavior() { return this._behavior; } incrementBehavior() { this._behavior++; } }
The code below shows the Cat class that will inherit information from the Animal class.
class Cat { constructor(name, usesLitter) { this._name = name; this._usesLitter = usesLitter; this._behavior = 0; } get name() { return this._name; } get behavior() { return this._behavior; } get usesLitter() { return this._usesLitter; } incrementBehavior() { this._behavior++; } }
To the right, in main.js, you will put what you learned to practice by creating a parent class named HospitalEmployee.
class HospitalEmployee { constructor(name){ this._name = name; this._remainingVacationDays = 20; } get name() { return this._name; } get remainingVacationDays() { return this._remainingVacationDays; } takeVacationDays(daysOff) { this._remainingVacationDays -= daysOff; } }
'Study > JavaScript' 카테고리의 다른 글
Inheritance IV (0) 2020.05.08 Inheritance III (0) 2020.05.08 Inheritance I (0) 2020.05.08 Method Calls (0) 2020.05.08 Object.keys() (0) 2020.05.08