class 类

class 类

ES6 提出 Class(类)这个概念,作为对象的模板。通过 class 关键字,可以定义类。
知识点:

  1. class 声明类
  2. constructor 定义构造函数初始化
  3. extends 继承父类
  4. super 调用父级构造方法
  5. static 定义静态方法和属性
  6. 父类方法可以重写
//父类
class Phone {
 //构造方法
 constructor(brand, color, price) {
 this.brand = brand;
 this.color = color;
 this.price = price;
 }
 //对象方法
 call() {
 console.log('我可以打电话!!!')
 } }
//子类
class SmartPhone extends Phone {
 constructor(brand, color, price, screen, pixel) {
 super(brand, color, price);
 this.screen = screen;
 this.pixel = pixel;
 }
 //子类方法
 photo(){
 console.log('我可以拍照!!');
 }
 playGame(){
 console.log('我可以玩游戏!!');
 }
 //方法重写
 call(){
 console.log('我可以进行视频通话!!');
 }
 //静态方法
 static run(){
 console.log('我可以运行程序')
 }
 static connect(){
 console.log('我可以建立连接')
 } }
 //实例化对象
const Nokia = new Phone('诺基亚', '灰色', 230);
const iPhone6s = new SmartPhone('苹果', '白色', 6088, 
'4.7inch','500w');
//调用子类方法
iPhone6s.playGame();
//调用重写方法
iPhone6s.call();
//调用静态方法
SmartPhone.run();

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值