Dart OOP

class

void main() {
  Idol blackPink = Idol();
  
  print(blackPink.name);
  print(blackPink.members);
  blackPink.sayHello();
  blackPink.introduce();
  
}

class Idol {
  String name = '블랙핑크';
  List<String> members = ['제니', '지수', '로제', '리사'];
  
  void sayHello() {
    print('안녕하세요 !');
  }
  
  void introduce() {
    print('멤버입니다.');
  }
}

constructor

void main() {
  Idol blackPink = Idol('블랙핑크', ['제니', '로제', '리사', '지수']);

  print(blackPink.name);
  print(blackPink.members);
  blackPink.sayHello();
  blackPink.introduce();

  Idol redVelvet = Idol('레드벨벳', ['아이린', '조이', '슬기', '웬디', '예리']);
  print(redVelvet.name);
  redVelvet.sayHello();
  redVelvet.introduce();
}

class Idol {
  String name;
  List<String> members;

  Idol(String name, List<String> members)
      : this.name = name,
        this.members = members;

  void sayHello() {
    print('안녕하세요 ! ${this.name} 입니다');
  }

  void introduce() {
    print('멤버입니다. ${this.members}');
  }
}

named constructor

void main() {
  Idol blackPink = Idol('블랙핑크', ['제니', '로제', '리사', '지수']);

  print(blackPink.name);
  print(blackPink.members);
  blackPink.sayHello();
  blackPink.introduce();

  Idol redVelvet = Idol.fromList(
    [['아이린', '조이', '슬기', '웬디', '예리'],'레드벨벳', ]
    );
  print(redVelvet.name);
  redVelvet.sayHello();
  redVelvet.introduce();
}

class Idol {
  String name;
  List<String> members;
  
  Idol(this.name, this.members);

  Idol.fromList(List values)
    : this.members = values[0],
  this.name = values[1];

  void sayHello() {
    print('안녕하세요 ! ${this.name} 입니다');
  }

  void introduce() {
    print('멤버입니다. ${this.members}');
  }
}

immutable 프로그래밍

  • final

  • const constructor

    • build time 변수 알 수 있다.

    • 내용이 동일하면 같은 객체, 내용이 다르면 다른 객체

void main() {
  Idol blackPink = const Idol('블랙핑크', ['제니', '로제', '리사', '지수']);

  print(blackPink.name);
  print(blackPink.members);
  blackPink.sayHello();
  blackPink.introduce();

  Idol redVelvet = Idol.fromList([
    ['아이린', '조이', '슬기', '웬디', '예리'],
    '레드벨벳',
  ]);
  print(redVelvet.name);
  redVelvet.sayHello();
  redVelvet.introduce();
}

class Idol {
  final String name;
  final List<String> members;

  const Idol(this.name, this.members);

  Idol.fromList(List values)
      : this.members = values[0],
        this.name = values[1];

  void sayHello() {
    print('안녕하세요 ! ${this.name} 입니다');
  }

  void introduce() {
    print('멤버입니다. ${this.members}');
  }
}

getter, setter

void main() {
  Idol blackPink = const Idol('블랙핑크', ['제니', '로제', '리사', '지수']);

  Idol redVelvet = Idol.fromList([
    ['아이린', '조이', '슬기', '웬디', '예리'],
    '레드벨벳',
  ]);
  
  print(redVelvet.firstMember);
  redVelvet.firstMember = 'soobin';
  
  print(redVelvet.firstMember);
}

class Idol {
  final String name;
  final List<String> members; // List는 final이여도 값을 바꿀 수 있음

  const Idol(this.name, this.members);

  Idol.fromList(List values)
      : this.members = values[0],
        this.name = values[1];

  void sayHello() {
    print('안녕하세요 ! ${this.name} 입니다');
  }

  void introduce() {
    print('멤버입니다. ${this.members}');
  }
  
  // getter
  String get firstMember {
    return this.members[0];
  }
  
  // setter -> 잘 안쓰임
  set firstMember(String name) {
   this.members[0] = name;
  }
}

private 변수

  • _ 붙이면 프라이빗

    • 클래스

    • 함수

    • 변수

void main() {
  _Idol blackPink = const _Idol('블랙핑크', ['제니', '로제', '리사', '지수']);

  _Idol redVelvet = _Idol.fromList([
    ['아이린', '조이', '슬기', '웬디', '예리'],
    '레드벨벳',
  ]);
  
  print(redVelvet.firstMember);
  redVelvet.firstMember = 'soobin';
  
  print(redVelvet.firstMember);
}

class _Idol {
  final String name;
  final List<String> members; // List는 final이여도 값을 바꿀 수 있음

  const _Idol(this.name, this.members);

  _Idol.fromList(List values)
      : this.members = values[0],
        this.name = values[1];

  void sayHello() {
    print('안녕하세요 ! ${this.name} 입니다');
  }

  void introduce() {
    print('멤버입니다. ${this.members}');
  }
  
  // getter
  String get firstMember {
    return this.members[0];
  }
  
  // setter -> 잘 안쓰임
  set firstMember(String name) {
   this.members[0] = name;
  }
}

상속

  • this → 나 자신 지칭

  • super → 부모 지칭

void main() {
  print('-------- Idol --------');
  Idol redVelvet = Idol(name: '레드벨벳', membersCount: 5);

  redVelvet.sayName();
  redVelvet.sayMembersCount();
  
  print('-------- Boy Group --------');
  BoyGroup bts = BoyGroup('BTS', 7);
  bts.sayName();
  bts.sayMembersCount();
  
  print('-------- Girl Group --------');
  GirlGroup newjeans = GirlGroup('NewJeans', 5);
  newjeans.sayName();
  newjeans.sayMembersCount();
  newjeans.sayGirlGroup();
  
  print('-------- Type Comparision --------');
  print(newjeans is Idol);
  print(newjeans is BoyGroup);
  print(newjeans is GirlGroup);
}

class Idol {
  String name;
  int membersCount;

  Idol({
    required this.name,
    required this.membersCount,
  });

  void sayName() {
    print('전 ${this.name}입니다.');
  }

  void sayMembersCount() {
    print('${this.name}${this.membersCount}명의 멤버가 있습니다.');
  }
}

class BoyGroup extends Idol {
  BoyGroup(
    String name,
    int membersCount,
  ) : super(name: name, membersCount: membersCount);
  
  void sayBoyGroup() {
    print('남자 아이돌 입니다.');
  }
}

class GirlGroup extends Idol {
  GirlGroup(
    String name,
    int membersCount,
  ) : super(name: name, membersCount: membersCount);
  
  void sayGirlGroup() {
    print('여자 아이돌 입니다.');
  }
}

method overriding

  • method - function(class 내부에 있는 함수)

  • override - 덮어쓰다

void main() {
  TimesTwo tt = TimesTwo(2);
  print(tt.calculate());
  
  TimesFour tf = TimesFour(2);
  print(tf.calculate());
}

class TimesTwo {
  final int num;

  TimesTwo(
    this.num,
  );

  int calculate() {
    return num * 2;
  }
}

class TimesFour extends TimesTwo {
  TimesFour(
    int num,
  ) : super(num);
  
  @override
  int calculate() {
    return super.num * 4;
  }
}
  • super.calculate() * 2;

void main() {
  TimesTwo tt = TimesTwo(2);
  print(tt.calculate());
  
  TimesFour tf = TimesFour(2);
  print(tf.calculate());
}

class TimesTwo {
  final int num;

  TimesTwo(
    this.num,
  );

  int calculate() {
    return num * 2;
  }
}

class TimesFour extends TimesTwo {
  TimesFour(
    int num,
  ) : super(num);
  
  @override
  int calculate() {
    return super.calculate() * 2;
  }
}

static

void main() {
  Employee yerin = Employee('예린');
  Employee hanni = Employee('하니');
  
  yerin.name = '백예린';
  yerin.printNameAndBuilding();
  hanni.printNameAndBuilding();
  
  Employee.building = '아남타워';
  yerin.printNameAndBuilding();
  hanni.printNameAndBuilding();
  
  Employee.pringBuilding();
}

class Employee {
  // static은 instance에 귀속되지 않고 class에 귀속된다. 
  static String? building;
  
  // 알바생 이름
  String name;
  
  Employee(
  this.name);
  
  void printNameAndBuilding() {
    print('이름 : $name');
    print('건물 : $building');
  } 
  
  static void pringBuilding(){
    print('저는 $building 건물에서 근무중입니다. ');
  }
}

interface

  • 특수한 구조를 강제하기 위함

void main() {
  BoyGroup bts = BoyGroup('BTS');
  GirlGroup newjeans = GirlGroup('New Jeans');
  
  bts.sayName();
  newjeans.sayName();
}

class IdolInterface {
  String name;
  
  IdolInterface(
    this.name
  );
  
  void sayName() {}
}

// 상속, 구현
// extends, implements
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 입니다. ');
  }
}
  • abstract

void main() {
	IdolInterfave test = IdolInterface('Black Pink');
}

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 입니다. ');
  }
}
void main() {
  BoyGroup bts = BoyGroup('BTS');
  GirlGroup newjeans = GirlGroup('New Jeans');
  
  bts.sayName();
  newjeans.sayName();
  
  print(newjeans is IdolInterface);
  print(newjeans is GirlGroup);
}

// interface
abstract class IdolInterface {
  String name;
  
  IdolInterface(
    this.name
  );
  
  void sayName() {}
}

// 상속, 구현
// extends, implements
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 입니다. ');
  }
}

Generic

void main() {
  Lecture<String> lecture1 = Lecture('123', 'lecture1');
  
  lecture1.printIdType();
  
  Lecture<int> lecture2 = Lecture(456, 'lecture2');
  lecture2.printIdType();
}

class Lecture<T> {
  final T id;
  final String name;
  
  Lecture(this.id, this.name);
  
  void printIdType() {
    print(id.runtimeType);
  }
}

OOP

Last updated