简体   繁体   English

如何设置属于基类[继承]的子类对象的属性(属性)? (将Base的attr传递给Child)

[英]How to set properties(attributes) of a Child class object which belong to Base Class [Inheritance]? (Pass Base's attr to Child)

I am new to classes and OOP and was following a tutorial explaining classes and inheritance and stumbled upon something. 我是班级和OOP的新手,并且正在关注解释类和继承的教程,偶然发现了一些东西。

How can I pass the values that belong to Base class' constructor into the child class? 如何将属于基类的构造函数的值传递给子类?

For example if a BASE class has 例如,如果BASE类具有

this.name=name
this .age=age

and the child class inherits Base class 子类继承基类

In Base class,there are two properties 在基类中,有两个属性

this.childSchoool=school
this.childfiends=friends

I can access the functions of Base Class using super().func; 我可以使用super().func;访问基类的super().func;

if I make a child class object like 如果我做一个子类对象

let childobj = new child("school_name", 6);

It'll initialise the child object with schoolname and age but 它将使用学校名称和年龄来初始化子对象,但是

HOW can I set name and age of the child class object? 如何设置子类对象的nameage

you have to provide method to do so: 您必须提供这样做的方法:

 class Animal { constructor(name) { this.name = name; } setName = (name) => this.name = name; } class Cat extends Animal { constructor(name) { super(name); } } const myCat = new Cat('Tom'); console.log(myCat.name); // Logs 'Tom' myCat.setName('Silvester'); console.log(myCat.name); // Logs 'Silvester' 

How can I pass the values that belong to Base class' constructor into the child class? 如何将属于基类的构造函数的值传递给子类?

You do it through super() which calls the Base class constructor to initialise the properties inherited from the Base class. 您可以通过super()来执行此操作,该调用Base类的constructor以初始化从Base类继承的属性。

 class Base{ constructor(name, age){ this._name = name; this._age = age; } } class Child extends Base{ constructor(name, age, schoolname){ super(name, age); //initializes the _name & _age properties this.schoolname = schoolname; } get name(){ return this._name; } set name(name){ this._name = name; } get age(){ return this._age; } set age(age){ this._age = age; } //getter setter for schoolname } const child = new Child("foo", 42, "bar"); console.log(child.name); console.log(child.age); child.name = "baz" console.log(child.name); 

In the above code the super(name, age) call in the Child class calls the constructor(name, age) of the Base class. 在上面的代码中, Child类中的super(name, age)调用调用了Base类的constructor(name, age)

Now in the constructor the this._name & this._age is set, but the this here is actually the object of the Child class. 现在在构造函数中设置了this._namethis._age ,但是这里的this实际上是Child类的对象。

When you invoke the new Child("foo", 42, "bar") a new object is created, then the this is assigned to the new object. 当您调用new Child("foo", 42, "bar")将创建一个新对象,然后this对象分配给新对象。 In the super(name, age) call the parameters name and age are used to initialise the this object so that the child instance will have the _name and _age properties from the Base class initialised with these values. super(name, age)调用中,参数nameage用于初始化this对象,以便child实例具有使用这些值初始化的Base类的_name_age属性。

Quite simply, if you have a class (classB) that is inheriting from another (classA), in the classB constructor you will take in parameters for the classA constructor and pass them along. 简而言之,如果您有一个从另一个(classA)继承的类(classB),则在classB构造函数中,您将为classA构造函数接受参数并将其传递。

MDN explains this. MDN对此进行了解释。

class Base {
  constructor(name, age){
    this.name = name;
    this.age = age;
  }
}

class Child extends Base {
  constructor(name, age, school, friends){
    super(name, age); //pass along to the Base constructor
    this.school = school;
    this.friends = friends;
  }
}

let achild = new Child('ben', 4, 'School1', 99);

Parameter order matters. 参数顺序很重要。 However, you can also use named parameters: 但是,您也可以使用命名参数:

let bchild = new Child(school='school2', age=9, name='chris', friends=22);

Or you can use spread syntax to pass in a map or object: 或者,您可以使用传播语法来传递地图或对象:

let amap = new Map([['name', 'sally'],['school', 'school1'],['age', 10],['friends', 9001]]);

let cchild = new Child(...amap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM