ES6类继承不继承

时间:2018-05-12 19:32:18

标签: javascript class inheritance

我正在通过ES6视频教程并重新创建用例以更好地理解语法。

关于类继承的主题。我创建了一个由Employee扩展的超类Person,我还有一个扩展Employee的类Intern。我有一个名为' doWork()'在实习生课程中返回' pay'的Person课程中,我试图覆盖' doWork'通过让它返回' free'。

我将康纳指定为新员工并成为新的实习生。问题是在我调用doWork()后,我只看到了“实习生自由”。在来自beth.doWork()的console.log中。不应该connor.doWork()返回'员工支付'既然员工正在扩展班级人员?

class Person {
    constructor(name) {
        this.name = name;
    }
    get name() {
        return this._name;
    }
    set name(newValue) {
        if(newValue) {
            this._name = newValue;
        }
    }
    doWork() {
        return 'Paid Employee';
    }
}

class Employee extends Person {
    constructor(title, name) {
        super(name);
        this._title = title;
    }
    get title() {
        return this._title;
    }
}

class Intern extends Employee {
    doWork() {
        return 'Intern Free'
    }
}

let makeEveryoneWork = function(...people) {
    var results = [];
    for(var i = 0; i < people.length; i++) {
        results.push(people[i].doWork());
    }
    return results
}

let connor = new Employee("Doctor", "Connor");
let beth = new Intern("Student", "Beth");

connor.doWork();  //expecting to be 'Employee Paid'
beth.doWork();  // exprecting to be 'Intern Free'

0 个答案:

没有答案