- 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类的实例。
更多回答
网站公告
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
