🥖 Bread Basics/Flutter

mixin, sealed, base 클래스

BreadDev 2025. 4. 25. 17:29
728x90
void main() {}

// Mixin Class
// 1) mixin은 extended나 with을 사용할 수 없다. 그렇기 때문에 mixin class도
//    마찬가지로 사용 불가능하다
// 2) 클래스는 on 키워드를 사용할 수 없다. 그렇기 때문에 mixin class도 on 키워드를 사용할 수 없다.
mixin class AnimalMixin {
  String bar() {
    return '멍멍';
  }
}

class Dog with AnimalMixin{}

// sealed 클래스는 abstract이면서 final이다
// 그리고 패턴매칭을 사용 할 수 있도록 해준다
sealed class Person3 {}

class Idol extends Person3 {}

class Engineer extends Person3 {}

class Chef extends Person3 {}

String whoIsHe(Person3 person3) => switch (person3) {
  Idol i => '아이돌',
  Engineer e => '엔지니어',
  _ => '나머지',
};

// interface로 선언하면 implement만 가능
interface class Person2 {
  final String name;
  final int age;

  Person2({required this.name, required this.age});
}

// base로 클래스를 선언하면
// extends는 가능하지만 implement는 불가능
// base, sealed, final로 선언된 클래스만 extend가 가능하다
base class Person1 {
  final String name;
  final int age;

  Person1({required this.name, required this.age});
}

// final로 클래스를 선언하면
// extends, implement, 또는 mixin으로 사용이 불가능
final class Person {
  final String name;
  final int age;

  Person({required this.name, required this.age});
}