首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript类,继承

Javascript类,继承
EN

Stack Overflow用户
提问于 2020-09-16 07:07:44
回答 2查看 50关注 0票数 1

我需要一些关于以下代码行的帮助:

代码语言:javascript
复制
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)

当我在终端中运行它时,我得到以下结果:宠物{ raceName:‘哺乳动物’,动物:未定义,petName:‘冥王星’,生日:'2000/

为什么我的动物没有定义?我希望在那里有“狗”这个字符串

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-16 07:16:40

你的Animal类需要2个参数,所以如果你只发送一个参数,它将使第二个参数成为undefined。您可以设置一个默认值,如果没有输入任何值,则将其设置为非undefined

代码语言:javascript
复制
class Race{
        constructor(_raceName){
            this.raceName = _raceName;
        }
    }

class Animal extends Race{
        constructor(_raceName, _animal="dog") {
            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)

而且,您必须更改您的宠物类,如下所示,才能使其正确。默认情况下,它将接受"dog",你可以根据你的实现来输入你的动物类。

代码语言:javascript
复制
class Race{
        constructor(_raceName){
            this.raceName = _raceName;
        }
    }

class Animal extends Race{
        constructor(_raceName, _animal="dog") {
            super(_raceName, _animal);
            this.animal = _animal;
        
        }
    }

class Pet extends Animal{
        constructor(_petName,_animalClass, _Birthday, _animal){
            super(_animal,_animalClass)
            this.petName = _petName;
            this.Birthday = _Birthday;
        }
    
    }

var Pet1 = new Pet("Pluto","cat", "2000/10/01", "Mamals")
console.log(Pet1)

票数 0
EN

Stack Overflow用户

发布于 2020-09-17 15:14:51

我想您需要添加一个额外的类来定义实例是一条狗。

代码语言:javascript
复制
class Race{
        constructor(_raceName){
            this.raceName = _raceName;
        }
    }

class Animal extends Race{
        constructor(race, _animal) {
            super(race);
            this.animal = _animal;
        }
    }

class Pet extends Animal{
        constructor(_petName, _Birthday, race, _animal){
            super(race, _animal);
            this.petName = _petName;
            this.Birthday = _Birthday;
        }
    }
    
class Dog extends Pet {
  constructor(...params) {
    super(...params, "dog");
  }
}

var Pet1 = new Dog("Pluto", "2000/10/01", "Mamals")
console.log(Pet1)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63910989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档