개발이 좋아서/Flutter가 좋아서

[Dart] 3일차_객체지향_상속_interface

zoaseo 2022. 12. 14. 13:45

1)

void main() {
  BoyGroup bts = BoyGroup('BTS');
  GirlGroup redVelvet = GirlGroup('레드벨벳');

  bts.sayName();
  redVelvet.sayName();

  print(bts is IdolInterface);
  print(bts is BoyGroup);
  print(bts is GirlGroup);
}

// interface
abstract class IdolInterface {
  String name;

  IdolInterface(
    this.name,
  );

  void sayName() {}
}

class BoyGroup implements IdolInterface {
  String name;

  BoyGroup(
    this.name,
  );

  void sayName() {
    print('제 이름은 $name입니다.');
  }
}

class GirlGroup implements IdolInterface {
  String name;

  GirlGroup(
    this.name,
  );

  void sayName() {
    print('제 이름은 $name입니다.');
  }
}