개요


Flutter를 공부하기 위한 Dart 공부

클래스


flutter는 클래스로 이루어져있음

클래스 선언


class Player{
  String name = '민돌';
  int xp = 100;

  void hello(){
    print("Hi my name is $name");
  }
}

객체 생성


void main(){
  var player1 = Player();
  Player player2 = Player();

  player1.name = 'mindol';
  player2.name = '민돌2';

  print(player1.name);
  print(player2.name);
  player1.hello();
  player2.hello();
}
// 결과
mindol
민돌2
Hi my name is mindol
Hi my name is 민돌2
Exited

생성자


class Player{
  late String name;
  late int xp;

  Player(String name, int xp){
    this.name = name;
    this.xp = xp;
  }

  void hello(){
    print("Hi my name is $name");
  }
}

void main(){
  var player1 = Player('mindol', 100);
  Player player2 = Player('민돌2', 1000);

  print(player1.name);
  print(player2.name);
  player1.hello();
  player2.hello();
}

기본 생성자 코드는 이렇지만 dart에서는 더 나은 방법을 추천

class Player{
  String name;
  int xp;

  Player(this.name, this.xp);

  void hello(){
    print("Hi my name is $name");
  }
}

생성자의 매개변수를 this로 지정해주면 선언 순서에 따른 변수 지정이 가능
(첫번째 매개변수는 name으로, 두번째 매개변수는 xp로)

Named Constructor Parameters


함수에서 Named Parameter를 지정했던 것처럼 class에서도 Named Parameter를 지정할 수 있음

class Player{
  String name;
  int xp;

  Player({required this.name, required this.xp});

  void hello(){
    print("Hi my name is $name");
  }
}

void main(){
  var player1 = Player(name: 'mindol', xp: 100);
  Player player2 = Player(name: '민돌2', xp: 1000);
}

또다른 생성자 방법

class Player{
  final String name;
  int xp, age;
  String team;

  Player({required this.name, required this.xp, required this.team, required this.age,});

  Player.CreateBluePlayer({required String name, required int age,}) : 
	this.name = name, this.age = age, this.xp = 0, this.team = 'blue';

  Player.CreateRedPlayer({required String name, required int age,}) : 
	this.name = name, this.age = age, this.xp = 0, this.team = 'red';

  void hello(){
    print("Hi my name is $name");
  }
}

void main(){
  var BluePlayer = Player.CreateBluePlayer(name: '민돌', age: 25);
  Player RedPlayer = Player.CreateBluePlayer(name: '민돌2', age: 25);
}

클래스 프로퍼티에 JsonData 셋팅


class Player{
  final String name;
  int xp;
  String team;

  Player.fromJson(Map<String, dynamic> playerJson) : 
	name = playerJson['name'], xp = playerJson['xp'], team = playerJson['team'];

  void hello(){
    print("Hi my name is $name");
  }
}

void main(){
  var apiData = [
    {
      "name" : "민돌",
      "team" : "red",
      "xp" : 0
    },
    {
      "name" : "민돌2",
      "team" : "blue",
      "xp" : 0
    },
    {
      "name" : "민돌3",
      "team" : "green",
      "xp" : 0
    }
  ];

	// foreach문 List.forEach((변수){코드});
  apiData.forEach((JsonData) {
    var player = Player.fromJson(JsonData);
    player.hello();
  });
}

Cascade Notation


class Player{
  String name;
  int xp;
  String team;

  Player({required this.name, required this.xp, required this.team});

  void hello(){
    print("Hi my name is $name");
  }
}

void main(){
  var mindol = Player(name: '민돌', xp: 1200, team: 'read');
  mindol.name = 'mindol';
  mindol.xp = 120000;
  mindol.team = 'blue';
	mindol.hello();
}

class Player{
  String name;
  int xp;
  String team;

  Player({required this.name, required this.xp, required this.team});

  void hello(){
    print("Hi my name is $name");
  }
}

void main(){
  var mindol = Player(name: '민돌', xp: 1200, team: 'read')
  ..name = 'mindol'
  ..xp = 120000
  ..team = 'blue'
	..hello();
}

와 같은 역할이다
💡 ..name, ..xp, ..team의 맨 앞에 붙는 .은 객체를 가르킴

Enum


enum Team { red, blue}

class Player{
  String name;
  int xp;
  Team team;

  Player({required this.name, required this.xp, required this.team});

  void hello(){
    print("Hi my name is $name");
  }
}

void main(){
  var mindol = Player(name: '민돌', xp: 1200, team: Team.red);
}

추상화 메소드 (abstract method)


추상화 클래스 특징

  • 추상화 클래스로는 객체를 생성할 수 없음
  • 추상화 클래스는 상속 받은 클래스들이 직접 구현해야하는 메소드를 모아 놓은 일종의 블루프린트
  • 특정 메소드를 구현하도록 강제함
  • 각 클래스들이 같은 함수에 다른 내용을 가질 수 있음 ```dart abstract class Human{ // 추상 클래스 선언 void walk(); }

class Player extends Human{ // 추상 클래스 상속 String name; int xp; Team team;

Player({required this.name, required this.xp, required this.team});

void walk() => print(“I’m Walking”); // 추상 클래스 메소드 구현

void hello(){ print(“Hi my name is $name”); } }

class Coach extends Human{ void walk() => print(“coach is walk”); }


## 상속
--------------
상속받은 클래스(자식클래스)는 부모 클래스 내의 모든 내용을 가져옴
```dart
class Human{
  final String name;
  Human({required this.name});
  void sayHello() => print("Hi my name is $name");
}

enum Team{blue, red}

class Player extends Human{
  final Team team;
  Player({required this. team, required String name}) : super(name: name);
}

void main(){
  var player = Player(team: Team.red, name: 'mindol');
}

💡 super 키워드를 통해 부모 클래스와 상호작용이 가능
super() ← 부모 클래스 생성자
부모 클래스의 메소드를 수정하고 싶을 경우 @override 키워드 사용

class Human{
  final String name;
  Human({required this.name});
  void sayHello() => print("Hi my name is $name");
}

enum Team{blue, red}

class Player extends Human{
  final Team team;
  Player({required this. team, required String name}) : super(name: name);
  @override
  void sayHello()
  {
    super.sayHello();
    print("and I play for $team");
  }
}

Mixin 클래스


Mixin : 생성자가 없는 클래스, 클래스에 프로퍼티들을 추가하거나 할 때 주로 사용
(다른 클래스의 프로퍼티와 메소드를 그냥 긁어오는 것 뿐, 상속과는 다른 개념(부모 클래스가 되지 않음)
여러 클래스에 사용이 가능하며, 생성자가 없어야 함

class Strong{
  final double strengthLevel = 1500.99;
}

class QuickRunner{
  void runQuick(){
    print("ruuuuuun");
  }
}

enum Team{blue, red}

class Player with Strong, QuickRunner{
  final Team team;
  Player({ required this. team, required String name});
}

class Horse with Strong, QuickRunner{}