题解 | #Getter#

Getter

http://www.nowcoder.com/practice/e7f3a2f429d945e49f5b48ef1065beda

26_Getter

本题考点:class类、getter

根据题目要求,当通过“Rectangle”类创建一个实例的时候,调用该实例的“area”属性返回矩形的面积,核心步骤有:

  1. 构造函数中需要两个参数分别为“width”和“height”
  2. 可以选择创建一个返回“width”与“height”乘积的方法
  3. 对“area”函数使用“get”计算,该函数返回第二步的方法

参考答案

class Rectangle {
    constructor(height, width) {
        this.height = height
        this.width = width
    }
    get area() {
        return this.calcArea()
    }
    calcArea() {
        return this.height * this.width
    }
}

全部评论

相关推荐

A_SOUL_Off...:疑似加班加出幻觉了
点赞 评论 收藏
分享
用微笑面对困难:你出于礼貌叫了人一声大姐,大姐很欣慰,她真把你当老弟
点赞 评论 收藏
分享
评论
9
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务