Javascript 如何在从另一个构造函数类继承的方法中添加更多信息,而不使用新的构造函数类替换该信息?
我创建了一个具有属性和方法的构造类Person。然后,我创建了另一个构造函数类教师,它继承自Person并拥有更多属性。我想要的是恢复Person()的方法greeting()的信息,但也要从Teacher()的属性中添加更多信息。如果我什么也不做,它通常会继承greeting()方法,但我想添加更多信息,所以我想在Teacher()类构造函数中再次编写greeting()。实际情况是,教师类中的新方法greeting()替换了Person类中的另一个方法greeting()。 我要做的是补充,而不是一个接一个地替换 这是我的代码:Javascript 如何在从另一个构造函数类继承的方法中添加更多信息,而不使用新的构造函数类替换该信息?,javascript,class,object,inheritance,Javascript,Class,Object,Inheritance,我创建了一个具有属性和方法的构造类Person。然后,我创建了另一个构造函数类教师,它继承自Person并拥有更多属性。我想要的是恢复Person()的方法greeting()的信息,但也要从Teacher()的属性中添加更多信息。如果我什么也不做,它通常会继承greeting()方法,但我想添加更多信息,所以我想在Teacher()类构造函数中再次编写greeting()。实际情况是,教师类中的新方法greeting()替换了Person类中的另一个方法greeting()。 我要做的是补充,
class Person{
constructor(first, last, age, gender, interests){
this.name={
first,
last
};
this.age = age;
this.gender = gender;
this.interests = Array.isArray(interests) ? interests : [ interests ]
}
greeting(){
let hobbie = this.interests.join(', ');
return `Hi. I'm ${this.name.first} I am ${this.age} . I like ${hobbie}`;
}
farewell(){
return `${this.name.first} has left the building. Bye for now!`;
}
}
class Teacher extends Person{
constructor(first, last,age, gender, interests, subject, grade){
super(first, last,age,gender,interests);
this._subject = subject;
this.grade = grade;
}
*//here I wanted to add this information into the other that already exists .not replace on from another*
greeting(){
return `I am ${this._subject} teacher , I teach ${this.grade} grade `
}
get subject(){
return this._subject;
}
set subject(newSubject){
this._subject = newSubject;
}
}
let snape = new Teacher('Severus', 'Snape', 45, 'male',['Potions','anoying students','using Sectumsempra on my enimies'], 'Dark arts', 5)
您可以使用
super
调用家长的方法(就像您通过Teacher
调用Person
的构造函数一样),如下所示:
班级人员{
建造师(第一、最后、年龄、性别、兴趣){
this.name={
第一,
最后的
};
这个。年龄=年龄;
这个。性别=性别;
this.interests=Array.isArray(兴趣)?兴趣:[兴趣]
}
问候语(){
让hobbie=this.interests.join(',');
返回“嗨,我是${this.name.first}我是${this.age}。我喜欢${hobbie}”;
}
再见{
return`${this.name.first}已离开大楼。再见!`;
}
}
班主任延伸人{
建造师(第一名、最后一名、年龄、性别、兴趣、科目、年级){
超级(第一、最后、年龄、性别、兴趣);
这个._subject=subject;
这个。等级=等级;
}
//在这里调用super.greeting()
问候语(){
返回`${super.greeting()}我是${this.\u subject}老师,我教${this.grade}年级`
}
获取主题(){
将此返回。\u主题;
}
设置主题(新闻主题){
本._subject=newSubject;
}
}
让斯内普=新老师(‘西弗勒斯’、‘斯内普’、‘45岁’、‘男性’、[‘药剂’、‘给学生施法’、‘在我的埃尼米斯身上使用Sectumsempra’、‘黑暗艺术’、‘5))
console.log(snape.greeting())
您可以使用super
调用家长的方法(就像您通过Teacher
调用Person
的构造函数一样),如下所示:
班级人员{
建造师(第一、最后、年龄、性别、兴趣){
this.name={
第一,
最后的
};
这个。年龄=年龄;
这个。性别=性别;
this.interests=Array.isArray(兴趣)?兴趣:[兴趣]
}
问候语(){
让hobbie=this.interests.join(',');
返回“嗨,我是${this.name.first}我是${this.age}。我喜欢${hobbie}”;
}
再见{
return`${this.name.first}已离开大楼。再见!`;
}
}
班主任延伸人{
建造师(第一名、最后一名、年龄、性别、兴趣、科目、年级){
超级(第一、最后、年龄、性别、兴趣);
这个._subject=subject;
这个。等级=等级;
}
//在这里调用super.greeting()
问候语(){
返回`${super.greeting()}我是${this.\u subject}老师,我教${this.grade}年级`
}
获取主题(){
将此返回。\u主题;
}
设置主题(新闻主题){
本._subject=newSubject;
}
}
让斯内普=新老师(‘西弗勒斯’、‘斯内普’、‘45岁’、‘男性’、[‘药剂’、‘给学生施法’、‘在我的埃尼米斯身上使用Sectumsempra’、‘黑暗艺术’、‘5))
console.log(snape.greeting())
在Teacher
中,您可以使用super
调用父类的方法
问候语(){
返回${super.greeting()}我是${this.\u subject}老师,我教${this.grade}年级`;
}
请注意,这对静态方法不起作用。在
Teacher
中,可以使用super
调用父类的方法
问候语(){
返回${super.greeting()}我是${this.\u subject}老师,我教${this.grade}年级`;
}
请注意,这对静态方法不起作用。在教师中,您需要调用
super.greeting()
。在教师中,您需要调用super.greeting()
。