JavaScript class inheritance

In JavaScript, class inheritance is a mechanism that allows you to create a new class that inherits properties and methods from one or more parent classes. This allows you to build more complex object hierarchies and share common behavior and properties.

The following are several common class inheritance methods:

1. **Single Inheritance (Single Inheritance)**: A subclass can only inherit from one parent class.
   ```javascript
   class ChildClass extends ParentClass {      // definition of subclass    }    ```


2. **Multiple Inheritance (Multiple Inheritance)**: A subclass can inherit from multiple parent classes.
   In JavaScript, multiple inheritance is not natively supported, but the effect of multiple inheritance can be simulated by mixing in (mixin) mode.
   ```javascript
   class ChildClass extends Mixin(ParentClass1, ParentClass2) {      // Definition of subclass    }    ```


3. **Multilevel Inheritance (Multilevel Inheritance)**: A subclass can inherit from another subclass to form an inheritance hierarchy.
   ```javascript
   class ChildClass extends ParentClass {      // definition of subclass    }

   class GrandchildClass extends ChildClass {      // definition of grandchild class    }    ```


In inheritance, a subclass specifies the parent class it inherits from by using the `extends` keyword. Subclasses can access the properties and methods of the parent class, and can add their own properties and methods.

When a subclass defines a method with the same name as the parent class, the method of the subclass will override (override) the method of the parent class, thereby changing its behavior. Subclasses can use the `super` keyword to call the constructors, methods and access properties of the parent class.

Here is a simple example showing how to use inheritance to create a graph object hierarchy:

```javascript
class Shape {
  constructor(color) {
    this.color = color;
  }

  getColor() {
    return this.color;
  }

  area() {
    console.log("This method should be overridden by subclasses.");
  }
}

class Circle extends Shape {
  constructor(color, radius) {
    super(color);
    this.radius = radius;
  }

  area() {
    return Math.PI * this.radius * this.radius;
  }
}

class Rectangle extends Shape {
  constructor(color, width, height) {
    super(color);
    this.width = width;
    this.height = height;
  }

  area() {
    return this.width * this.height;
  }
}

// Create Circle and Rectangle instances
const circle = new Circle("red", 5);
const rectangle = new Rectangle("blue", 4, 6);

console.log(circle.getColor()); // output: red
console.log(circle.area()); // output: 78.53981633974483

console.log(rectangle.getColor()); // output: blue
console.log(rectangle.area()); // output: 24
```

In the above example, the `Circle` and `Rectangle` subclasses both inherit the properties and methods of the `Shape` parent class. Subclasses can be customized

certainly! Here's a more concrete example showing class inheritance in JavaScript:

```javascript
// 父类
class Animal {
  constructor(name) {
    this.name = name;
  }

  eat() {
    console.log(`${this.name} is eating.`);
  }
}

// The child class inherits the parent
class class Dog extends Animal {   constructor(name, breed) {     super(name);     this.breed = breed;   }



  bark() {
    console.log(`${this.name} is barking.`);
  }
}

// Create an instance of the subclass
const myDog = new Dog("Buddy", "Golden Retriever");

// Call the subclass method
myDog.eat(); // Output: Buddy is eating.
myDog.bark(); // Output: Buddy is barking.
console.log(myDog.name); // Output: Buddy
console .log(myDog.breed); // Output: Golden Retriever
```

In the example above, `Animal` is a parent class that has a `name` property and an `eat()` method. `Dog` is a subclass that inherits the `Animal` parent class through the `extends` keyword. The constructor in the subclass uses `super()` to call the constructor of the parent class and pass the corresponding parameters.

The subclass `Dog` also defines a `bark()` method, which is a subclass-specific behavior. We create an instance `myDog` of the `Dog` class, and call the `eat()` method of the parent class and the `bark()` method of the subclass. We can also access instance properties like `name` and `breed`.

Through inheritance, subclasses can inherit the properties and methods of the parent class, and can also add their own unique properties and methods. In this way, code reuse and extension can be realized, so that objects can share common behaviors and characteristics, and customize their own behaviors as needed.

Guess you like

Origin blog.csdn.net/smarten57/article/details/131134696