본문 바로가기

Dart

[Dart] Class - getter, setter

void main() {

  House house1 = new House(4,2);
  house1.aboutHouse();
  
  print(house1.rooms);
  house1.rooms = 10;
  house1.aboutHouse();
}

class House {
  // private variable
  int _rooms;
  int _floors;
  
  House(this._rooms, this._floors);
  
  void aboutHouse(){
    print('This house has $_floors floors and $_rooms rooms.');
  }
  
  int get rooms{
    return this._rooms;
  }
  
  set rooms(int room){
    this._rooms = room;
  }
} 

--------------------------------------------[result]

This house has 2 floors and 4 rooms.
4
This house has 2 floors and 10 rooms.

'Dart' 카테고리의 다른 글

[Dart] Class - Method override  (0) 2021.05.27
[Dart] Class - 상속/Inheritance  (0) 2021.05.27
[Dart] Class - 선언 및 생성자  (0) 2021.05.27
[Dart] Null Safety  (0) 2021.05.12
[Dart] Stream 처리.  (0) 2021.05.01