账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    JavaScript中子类继承父类,子类的一个实参是一个类,应该如何写constructor函数
    12
    0

    父类:

    class Person{
        constructor(name,age){
            this.name = name;
            this.age = age;
        }
    
        introduce(){
            return `My name is ${this.name}. I am ${this.age} years old.`;
        }
    }
    
    module.exports = Person;

    子类:

    import Person from "../../src/practice_7/person.js"
    
    class Teacher extends Person{
        constructor(name, age, klass){
            super(name, age);
            this. klass = klass;
        }
    
        introduce(){
            if(this.klass === undefined){
                return `${super.introduce()} I am a Teacher. I teach No Class.`
            }else{
                return `${super.introduce()} I am a Teacher. I teach Class ${this.klass}.`;
            }
            
        }
    
        introduceWith(studentJerry){
            if(studentJerry.klass === this.klass){
                return `${super.introduce()} I am a Teacher. I teach Jerry.`
            }else{
                return `${super.introduce()} I am a Teacher. I don't teach Jerry.`
            }
        }
    }
    
    module.exports = Teacher;

    测试函数

    "use strict";
    import _ from "lodash";
    import chai from "chai";
    import sinon from "sinon";
    import sinonChai from "sinon-chai";
    const expect = chai.expect;
    chai.use(sinonChai);
    
    import Person from "../../src/practice_7/person.js";
    import Student from "../../src/practice_7/student.js";
    import Teacher from "../../src/practice_7/teacher-option2.js";
    import Class from "../../src/practice_7/class.js";       
    
        describe("Teacher", () => {
            let klass;
    
            before(() => {
                klass = new Class(2);
            });
    
            it("should have field name, age and class number", () => {
                const teacher = new Teacher("Tom", 21, klass);
                expect(teacher.name).to.equal("Tom");
                expect(teacher.age).to.equal(21);
                expect(teacher.klass).to.equal(klass);
            });
         });
    
            describe("#introduce", () => {
                it.only("should overwrite Person introduce, introduce with name, age and class number, given teacher have class", () => {
                    const teacher = new Teacher("Tom", 21, klass);
                    const introduce = teacher.introduce();
    
                    expect(introduce).to.equal("My name is Tom. I am 21 years old. I am a Teacher. I teach Class 2.");
    
                });
    
                it("should overwrite Person introduce, introduce with name, age and class number, given teacher have no class", () => {
                    const teacher = new Teacher("Tom", 21);
                    const introduce = teacher.introduce();
    
                    expect(introduce).to.equal("My name is Tom. I am 21 years old. I am a Teacher. I teach No Class.");
    
                });
            });
    
            describe("#introduceWith", () => {
                let studentJerry;
    
                before(() => {
                    studentJerry = new Student("Jerry", 8, klass);
                });
    
                it("should return I am teaching some guy, given my class is same with this guy's class", () => {
                    const teacher = new Teacher("Tom", 21, klass);
                    const introduce = teacher.introduceWith(studentJerry);
    
                    expect(introduce).to.equal("My name is Tom. I am 21 years old. I am a Teacher. I teach Jerry.");
    
                });
    
                it("should return I am teaching some guy, given my class is different with this guy's class", () => {
                    const teacher = new Teacher("Tom", 21, new Class(10));
                    const introduce = teacher.introduceWith(studentJerry);
    
                    expect(introduce).to.equal("My name is Tom. I am 21 years old. I am a Teacher. I don't teach Jerry.");
    
                });
    });

    用npm test运行后,提示:

     + expected - actual
    
          -My name is Tom. I am 21 years old. I am a Teacher. I teach Class [object Object].
          +My name is Tom. I am 21 years old. I am a Teacher. I teach Class 2.
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 我在m城有车 普通会员 1楼

      在JavaScript中,子类继承父类时,可以通过this关键字来访问子类的实例。然后,你可以在子类的构造函数中设置this的值。例如:

      ```javascript class Parent { constructor(subClass) { this.subClass = subclass; } }

      class Child extends Parent { constructor(subClass) { super(subClass); } }

      // 创建一个Child类的实例 const child = new Child();

      // 访问Child类的实例 console.log(child.subClass); // 输出Child类的实例 ```

      在这个例子中,Child类继承了Parent类,因此它的this关键字指向Parent类的实例。当我们在Child类的构造函数中设置this的值时,this.subClass将指向Child类的实例。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部