es6 类与继承

简介: ES6 的类与继承特性使得 JavaScript 的面向对象编程更加规范和易于维护,提高了代码的可读性和可复用性,广泛应用于各种复杂的 JavaScript 应用程序开发中。

ES6 中的类与继承提供了一种更接近传统面向对象编程语言的语法糖,使得 JavaScript 中的面向对象编程更加清晰和易于理解。以下是对 ES6 类与继承的详细介绍:

类的定义

  • 使用 class 关键字来定义类,类中可以包含构造函数 constructor、实例方法、静态方法等。
class Person {
   
  constructor(name, age) {
   
    this.name = name;
    this.age = age;
  }

  sayHello() {
   
    console.log(`Hello, my name is ${
     this.name}.`);
  }

  static introduce() {
   
    console.log('This is a Person class.');
  }
}

在上述示例中,Person 类有一个构造函数用于初始化 nameage 属性,一个实例方法 sayHello 用于打印问候语,还有一个静态方法 introduce

类的实例化

  • 通过 new 关键字来创建类的实例。
const person = new Person('John', 30);
person.sayHello(); 
// 输出:Hello, my name is John.

继承

  • 使用 extends 关键字实现类的继承,子类可以继承父类的属性和方法,并可以添加自己的属性和方法。

    class Student extends Person {
         
    constructor(name, age, grade) {
         
      super(name, age);
      this.grade = grade;
    }
    
    study() {
         
      console.log(`${
           this.name} is studying in grade ${
           this.grade}.`);
    }
    }
    

    在这个例子中,Student 类继承自 Person 类,通过 super 关键字调用父类的构造函数来初始化继承的属性,并添加了自己的 grade 属性和 study 方法。

重写方法

  • 子类可以重写父类的方法,以实现自己特定的行为。
class Teacher extends Person {
   
  constructor(name, age, subject) {
   
    super(name, age);
    this.subject = subject;
  }

  sayHello() {
   
    console.log(`Hello, I'm ${
     this.name}, I teach ${
     this.subject}.`);
  }
}

const teacher = new Teacher('Alice', 35, 'Math');
teacher.sayHello(); 
// 输出:Hello, I'm Alice, I teach Math.

静态方法继承

  • 子类会继承父类的静态方法,但如果子类重写了父类的静态方法,那么子类的静态方法将覆盖父类的同名静态方法。
Student.introduce(); 
// 输出:This is a Person class.,因为 Student 类没有重写 introduce 方法

class AnotherStudent extends Person {
   
  static introduce() {
   
    console.log('This is AnotherStudent class.');
  }
}

AnotherStudent.introduce(); 
// 输出:This is AnotherStudent class.

类的访问器属性

  • ES6 类支持使用 getset 关键字来定义访问器属性,用于控制对类的属性的访问和修改。
class Circle {
   
  constructor(radius) {
   
    this._radius = radius;
  }

  get radius() {
   
    return this._radius;
  }

  set radius(newRadius) {
   
    if (newRadius > 0) {
   
      this._radius = newRadius;
    } else {
   
      console.log('Radius must be positive.');
    }
  }
}

const circle = new Circle(5);
console.log(circle.radius); 
// 输出:5

circle.radius = 10;
console.log(circle.radius); 
// 输出:10

circle.radius = -2;
// 输出:Radius must be positive.
console.log(circle.radius); 
// 输出:10

ES6 的类与继承特性使得 JavaScript 的面向对象编程更加规范和易于维护,提高了代码的可读性和可复用性,广泛应用于各种复杂的 JavaScript 应用程序开发中。

相关文章
|
物联网 Shell Swift
NPU推理&微调大模型实战
本文为魔搭社区轻量级训练推理工具SWIFT微调实战教程系列
成功解决UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 130: invalid continuation b
成功解决UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 130: invalid continuation b
成功解决UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 130: invalid continuation b
|
弹性计算 Cloud Native 数据可视化
99元建站+云服务器?我们认真的!
「云·原生建站 」+「云服务器ECS e实例」套餐低至0.2折,仅需99元/首年!复制链接直达购买页:https://market.aliyun.com/xinxuan/pinpai1
1271 0
99元建站+云服务器?我们认真的!
|
NoSQL Java Redis
【Redis】Zset有序类型基本使用
【Redis】Zset有序类型基本使用
218 0
【Redis】Zset有序类型基本使用
|
JSON JavaScript 前端开发
JQuery Tables 的应用(二)
【转贴地址】 https://blog.csdn.net/lovetea99/article/details/52026935 目标: 使用 jQuery Datatable 构造数据列表,并且增加或者隐藏相应的列,已达到数据显示要求。
1115 0
|
9天前
|
人工智能 运维 安全
|
7天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
8天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
689 23