Dart

Dart static 사용

꽃피는봄날 2021. 6. 13. 20:49

static

main() {
  Employee seulgi = new Employee('슬기');
  Employee chorong = new Employee('초롱');

  // static 변수 사용
  Employee.building = '위워크';

  print('--- 슬기 ---');
  seulgi.printNameAndBuilding();
  print('--- 초롱 ---');
  chorong.printNameAndBuilding();

  // static 변수 사용
  Employee.building = '강남 CGV';
  print('\n--- 슬기 ---');
  seulgi.printNameAndBuilding();
  print('--- 초롱 ---');
  chorong.printNameAndBuilding();

  print("");
  Employee.printBuilding();

}


class Employee{
  static String building ="";
  String name;

  Employee(String name) :
      this.name = name;

  void printNameAndBuilding(){
    print('제 이름은 $name 입니다. $building 건물에서 근무하고 있습니다.');
  }

  static void printBuilding(){
    print('저희 회사 직원들은 $building 건물에서 근무중 입니다.');
  }
}