반응형
Class
void main()
{
  // constructor
  Idol blackPink = const Idol(
    name:'블랙핑크',
    members:['지수', '제니', '리사', '로제']);
  
  blackPink.sayHello();
  blackPink.introduce();
  
  // named constructor
  Idol blackPink2 = Idol.fromList([
    ['지수', '제니', '리사', '로제'],
    '블랙핑크'
    ]);
  
  blackPink2.sayHello();
  blackPink2.introduce();
  
}
 
class Idol
{
  final String name;
  final List<String> members;
  
  // constructor
  const Idol({
    required this.name, 
    required this.members});
  
  // named constructor
  Idol.fromList(List values)
    : this.members = values[0], 
      this.name= values[1];
  
  // function
  void sayHello() {print('안녕하세요. ${this.name}입니다.');}
  
  void introduce() {print('저희 멤버는 ${this.members}가 있습니다.');}
  
  // getter, setter 굳이 사용 X, 기능적 차이 없음
}

클래스는 java와 똑같다.

  • 다만 클래스의 접근자가 private, public만 있다. 굉장히 마음에 든다.
  • private은 변수 앞에 (_)를 붙이면 된다. public은 안붙여도 됨.
  • getter, setter 굳이 사용해도 되지 않을 것 같다.

void main()
{
  Idol apink = Idol(name:'에이핑크', membersCount:5);
  
  apink.sayName();
  apink.sayMembersCount();
  
  print("--------------");
  
  BoyGroup bts = BoyGroup('BTS', 7);
  
  bts.sayMembersCount();
  bts.sayName();
}

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);
  
}

상속도 java와 똑같다. override, implement도 똑같으니 굳이 안따라해보고 넘어 간다.

void main()
{
  Idol apink = Idol(name:'에이핑크', membersCount:5);
  
  apink.sayName();
  apink.sayMembersCount();
  
  print("--------------");
  
  BoyGroup bts = BoyGroup('BTS', 7);
  
  bts.sayMembersCount();
  bts.sayName();
}

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);
  
}

 

Static

Class 귀속, Instance 귀속.

void main()
{
  Employee seulgi = Employee('슬기');
  Employee chorong = Employee('초롱');
  
  // instance 귀속
  seulgi.printNameAndBuilding();
  chorong.printNameAndBuilding();
  print("--------------------------");
  // class 귀속 : static
  Employee.building = '모르는';
  seulgi.printNameAndBuilding();
  chorong.printNameAndBuilding();
  Employee.printBuilding();
}

class Employee
{
  static String? building;
  final String name; 
  
  Employee(this.name);
  
  void printNameAndBuilding()
  {
    print('제 이름은 $name입니다. $building 건물에서 근무하고 있습니다.');
  }
  
  static void printBuilding()
  {
    print('저는 $building 건물에서 근무 중입니다.'); 
  }
}

 

Generic

외부에서 타입을 받아서 사용할 수 있음.

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


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

 

728x90
반응형

'Flutter > Dart' 카테고리의 다른 글

Dart Study #1 기본 문법  (0) 2022.08.04
Dart Study #3 futures, async, await  (0) 2022.07.23

+ Recent posts