Javascript类,继承
我需要以下代码行的帮助:Javascript类,继承,javascript,class,inheritance,Javascript,Class,Inheritance,我需要以下代码行的帮助: class Race{ constructor(_raceName){ this.raceName = _raceName; } } class Animal extends Race{ constructor(_raceName, _animal) { super(_raceName, _animal); this.animal = _an
class Race{
constructor(_raceName){
this.raceName = _raceName;
}
}
class Animal extends Race{
constructor(_raceName, _animal) {
super(_raceName, _animal);
this.animal = _animal;
}
}
class Pet extends Animal{
constructor(_petName, _Birthday, _animal){
super(_animal)
this.petName = _petName;
this.Birthday = _Birthday;
}
}
var Pet1 = new Pet("Pluto", "2000/10/01", "Mamals")
console.log(Pet1)
当我在终端中运行它时,我得到以下信息:
宠物{
种名:“Mamals”,
动物:未定义,
宠物名:'冥王星',
生日:'2000/
为什么我的动物未定义?我想让字符串“dog”在那里你的
动物
类需要两个参数,因此如果你只发送一个,它将使第二个未定义
。如果没有输入值,你可以设置一个默认值,使它不未定义
class竞赛{
构造函数(名称){
this.raceName=\u raceName;
}
}
类动物扩大了种族{
构造器(_raceName,_animal=“dog”){
超级(_raceName,_animal);
this.animal=\u动物;
}
}
宠物类动物{
构造器(_宠物名,_生日,_动物){
超级动物
this.petName=\u petName;
这个.生日=_生日;
}
}
var Pet1=新的Pet(“冥王星”、“2000/10/01”、“马马尔斯”)
console.log(Pet1)
您的Animal
类需要两个参数,因此,如果只发送一个参数,它将使第二个参数未定义
。您可以设置一个默认值,如果没有输入值,则默认值为非未定义
class竞赛{
构造函数(名称){
this.raceName=\u raceName;
}
}
类动物扩大了种族{
构造器(_raceName,_animal=“dog”){
超级(_raceName,_animal);
this.animal=\u动物;
}
}
宠物类动物{
构造器(_宠物名,_生日,_动物){
超级动物
this.petName=\u petName;
这个.生日=_生日;
}
}
var Pet1=新的Pet(“冥王星”、“2000/10/01”、“马马尔斯”)
log(Pet1)
我想您需要添加一个额外的类来定义实例是一只狗
class竞赛{
构造函数(名称){
this.raceName=\u raceName;
}
}
类动物扩大了种族{
建造师(种族、动物){
超级(种族);
this.animal=\u动物;
}
}
宠物类动物{
构造器(宠物名、生日、种族、动物){
超级(种族,动物);
this.petName=\u petName;
这个.生日=_生日;
}
}
宠物狗{
构造函数(…参数){
超级(…参数,“狗”);
}
}
var Pet1=新狗(“冥王星”、“2000/10/01”、“马玛尔斯”)
log(Pet1)
我想您需要添加一个额外的类来定义实例是一只狗
class竞赛{
构造函数(名称){
this.raceName=\u raceName;
}
}
类动物扩大了种族{
建造师(种族、动物){
超级(种族);
this.animal=\u动物;
}
}
宠物类动物{
构造器(宠物名、生日、种族、动物){
超级(种族,动物);
this.petName=\u petName;
这个.生日=_生日;
}
}
宠物狗{
构造函数(…参数){
超级(…参数,“狗”);
}
}
var Pet1=新狗(“冥王星”、“2000/10/01”、“马玛尔斯”)
console.log(Pet1)
明显的问题:Race构造函数接受一个参数…但Animal用两个参数调用它…Animal构造函数接受两个参数,但Pet用一个参数调用它,第二个问题,你想让Pet1成为一只狗
-但是字符串狗
在你的代码中不存在?Javascript不知道——就像Pluto
super
dog
……那么,你还需要将该字符串传递给某个明显的问题:种族构造函数接受一个参数……但动物用两个参数调用它……动物构造函数接受两个参数,但是Pet用第二个问题来调用它,你想让Pet1成为一只狗
-但是字符串狗
在你的代码中不存在?Javascript不知道冥王星是一只狗的著名名字我不太清楚我是否理解,你会怎么做?我会用正确的数字调用超级
参数的正确顺序-对于dog
…您还需要将该字符串传递到某个地方