使用 getter 和 setter 来控制对象的访问?-灵析社区

777七月

不太明白你要干什么 1. 你的 `constructor`,看起来是接收了一个华氏度然后转成摄氏度保存起来 那既然是**摄氏度** 为什么这个成员叫 `farenheit` 2. 你的 `set temperature` 没有接收参数,这是跑不起来的。 class Thermostat { constructor(farenheit) { this.celsius = 5/9 * (farenheit - 32) } get temperature() { return this.celsius } set temperature(celsius) { this.celsius = celsius; } } ![image.png](https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241103/3879c8bf3d873861975c57e5f1a4b576.png)

阅读量:1

点赞量:0

问AI