mixin class Strong{
  final double strengthLevel = 1500.99;
}

mixin class QuickRunner{
  void runQuick(){
    print("ruuuun");
  }
}

mixin class Tall{
  final double height = 1.99;
}
enum Team{blue, red}

//mixin은 with 사용. QuickRunner와 Strong 클래스의 프로퍼티와 메소드를
//Player에 사용할거임. 즉 with에 있는 클래스 기능 재사용 가능
class Player with Strong, QuickRunner, Tall{
  final Team team;
  Player({
    required this.team,
  });
}

void main(){
  var player = Player(team:Team.blue);
  player.runQuick();
  
}

 

mixin 클래스는 생성자가 없어야함. mixin이라고 기입해야 컴파일에러 없음.

상속/mixin 핵심 차이는 단일 vs 다중 그리고 is-a vs has-a 입니다.

상속 (extends)

  • 하나의 클래스만 상속 가능
  • 부모-자식 관계 (is-a)
  • Player is a Human (플레이어는 인간이다)

mixin (with)

  • 여러 개 동시에 사용 가능
  • 기능 재사용 관계 (has-a)
  • Player has Strong, QuickRunner, Tall (플레이어는 강함·빠름·큰 키를 가진다)

'DART&Flutter' 카테고리의 다른 글

Dart) 오버라이딩  (0) 2026.05.19
Dart) Cascade operator  (0) 2026.05.19
Dart) 이름있는 생성자(Named Constructors)  (0) 2026.05.19
Dart) 클래스 생성자 및 Named Parameters  (0) 2026.05.18
Dart) final vs const  (2) 2026.05.15

+ Recent posts