class类

class Potiny {
    constructor(x, y) { // constructor是类的默认方法,当我们通过new关键字调用类时,如果没有constructor方法就会默认给到一个,然后自动调用constructor方法
        this.x = x
        this.y = y
    }
    toString() {
       return this.x + this.y
    }
}
const p = new Potiny(2, 2) // 类必须使用new关键字调用 
console.log(p.toString()) // 4

class Potiny {
    constructor() {
        this.name = '小明'
        this.age = 28
    }
}
class PotinyChild extends Potiny { // 通过extends关键字,就可以做到类的继承
    say() {
        return this.name + '今年' + this.age + '岁'
    }
}
let p = new PotinyChild()
console.log(p.say()) // 小明今年28岁

 

posted @ 2022-01-24 16:43  格里兹曼  阅读(29)  评论(0编辑  收藏  举报