본문 바로가기
개발일지/임시카테고리

dart - class static (다트 - 클래스 스태틱)4

by 다니엘의 개발 이야기 2025. 3. 24.
320x100
// static

void main() {
  Employee daniel = Employee('daniel');
  Employee chajun = Employee('chajun');
  
  daniel.printNameAndBuilding();
  chajun.printNameAndBuilding();
  
//   제 이름은 daniel입니다. null 에서 근무 하고 있습니다.
//   제 이름은 chajun입니다. null 에서 근무 하고 있습니다.
  
  // static은 아예 그 고유의 변수 값 안에 값을 설정해주는 느낌이다.
  Employee.building = '대박타워';
  
  daniel.printNameAndBuilding();
  chajun.printNameAndBuilding();
  
//   제 이름은 daniel입니다. 대박타워 에서 근무 하고 있습니다.
//   제 이름은 chajun입니다. 대박타워 에서 근무 하고 있습니다.
  
}

class Employee {
  // the building where working employee
  static String? building;
  
  // employee's name
  final String name;
  
  Employee(
    this.name
  );
  
  void printNameAndBuilding() {
    print('제 이름은 $name입니다. $building 에서 근무 하고 있습니다.');
  }
  
  static void printBuilding() {
    print('저는 $building 건물에서 근무중 입니다.');
  }
}

 

300x250