본문 바로가기

Flutter/Dart

Dart- 클래스

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

  // named constructor 1
  Player({ // Dart에서 권장되는 Key:Value 쌍을 이용한 named constructor
      required this.name,
      required this.xp,
      required this.team,
      required this.age,});

  // named constructor 2
  Player.createBluePlayer({
    required String name,
    required int age,
  })  : this.age = age,
        this.name = name,
        this.team = 'blue',
        this.xp = 0;
 
  // named constructor 3
  Player.createRedPlayer(String name, int age)
      : this.name = name,
        this.xp = 0,
        this.team = "red",
        this.age = age;

  // Postitional constructor
  Player(this.name, this.xp, this.team, this.age);  
  
  // normal constructor
  Player(String name, int xp) {  
    this.name = name;
    this.xp = xp;
    this.team = team;
    this.age = age;
  }

  void sayHello() {
    print(
        "hello my name is ${name} myteam is $team, my xp is $xp and my age is +10 is ${age + 10}");
  }
}

void main() {
  var player1 = Player(
    name: "umjoonsik",
    team: "red",
    age: 10,
    xp: 150,
  );

  var player2 = Player.createRedPlayer("nico", 31);

  var player3 = Player.createBluePlayer(name: "tom", age: 80);

  player1.sayHello();
  player2.sayHello();
  player3.sayHello();
}

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

Dart- 함수  (0) 2024.03.18
Dart- 데이터 타입 정리  (0) 2024.03.17
Dart- 변수 정리  (0) 2024.03.17