当前位置: 首页 > article >正文

【ES6】ES6中的类

基础定义和使用

class Animal {
  constructor(name, species, age) {
    this.name = name
    this.species = species
    this.age == age
  }
}

let dog = new Animal("Spot", "Dog", 4)

私有变量

变量名前带#即可。

Getter 和Setter方法

继承

// 父类
class Point{
    constructor(x,y){
        this.x = x;
        this.y = y;
    }

    toString(){
        return this.x + ',' + this.y;
    }
}
// 子类
class ColorPoint extends Point{
    constructor(x,y,color){
        super(x,y); // 调用父类的构造函数
        this.color = color;
    }

    toString(){
        return this.color + ' ' + super.toString(); // 调用父类的toString()
    }
}

上面代码表示的类图关系如下:

在这里插入图片描述

let cp = new ColorPoint(100,100,"red"); // 创建实例

console.log(cp); // 控制台输出

重写

静态方法和静态属性


http://www.kler.cn/a/583087.html

相关文章:

  • QT5.9.2项目复制到新电脑上后“error: LNK2019: 无法解析的外部符号”错误
  • Vue:列表操作
  • nginx实现tomcat反向代理
  • vscode更新后: 适用于 Linux 的 Windows 子系统必须更新到最新版本才能继续。可通过运行 “wsl.exe --update” 进行更新
  • OpenHarmony子系统开发 - 模块配置规则
  • word处理控件Aspose.Words教程:使用 Python 删除 Word 中的空白页
  • HCIE学习是自学还是选择培训机构?
  • 二分查找易错点分析报告
  • Linux软件包管理与Vim编辑器指南
  • rust学习笔记13-18. 四数之和
  • 【Spring】@PostConstruct详解
  • Conda:CondaSSLError
  • LabVIEW VI Scripting实现连接器窗格自动化
  • varchar (255) varchar (2550) 在mysql中实际占的空间会是十倍吗
  • MySQL的安装、备份还原及主从同步
  • java设计模式之桥接模式
  • 深度学习GRU模型原理
  • Linux——Shell运行原理以及Linux权限
  • 【Linux docker 容器】关于想要让虚拟机在开机时候也docker自己启动,容器也自己启动,省去要自己开docker和容器
  • 已安装 MFC 仍提示“此项目需要 MFC 库”的解决方法 (MSB8041)