面向对象-getter-setter

// class Rect {
//   num heihgt;
//   num width;

//   Rect(this.heihgt, this.width);
//   area() {
//     return this.heihgt * this.width;
//   }
// }

class Rect {
  num heihgt;
  num width;

  Rect(this.heihgt, this.width);
  // area() {
  //   return this.heihgt * this.width;
  // }

  get area {
    return this.heihgt * this.width;
  }

  set areaHieght(value) {
    this.heihgt = value;
  }
}

void main() {
  Rect r = new Rect(10, 4);
  // print(r.area());
  print(r.area);

  r.areaHieght = 6;
  print(r.area);
}
posted @ 2021-07-28 20:03  13522679763-任国强  阅读(30)  评论(0)    收藏  举报