본문 바로가기
앱/Dart

Dart Mixins

by devved 2023. 2. 4.
반응형

Mixins

// Mixins : 생성자가 없는 클래스
// 클래스에 프로퍼티들을 추가하거나 할 때 사용
// 상속 개념 아니고 단순히 with 사용하여 mixin 내부의 프로퍼티와 메소드를 가져옴
class Strong {
  final double strengthLevel = 1500.99;
}

class QuickRunner {
  void runQuick() {
    print("ruuuuuuuun!");
  }
}

class Tall {
  final double height = 1.99;
}

// 요런식으로 여러 클래스에 재사용하려고 씀!!
class Player with Strong, QuickRunner, Tall {
  final team;

  Player({
    required this.team,
  });
}

class Horse with Strong, QuickRunner {}

class Kid with QuickRunner {}
반응형