classPlayer{String name ='moz';// class 에서 property 를 선언할 때는 타입을 사용한다. (↔ function 에서 variable 을 사용할 때는 var )finalString nickname ='moz';// 변경할 수 없게 final 로 선언
int xp =1500;voidsayHello(){print("Hi my name is $name");// this 를 사용하지 않아도 됨var name ='moz';print("Hi my name is ${this.name}");// 동일한 이름의 variable 이 method 내에 있을 경우는 this 로 참조 }}voidmain(){var player =Player();// new 를 붙여도 되지만 없어도 됨print(player.name);// moz
player.name ='tiq';print(player.name);// tiq
player.nickname ='tiq';// Error
player.sayHello();}
Constructor (생성자)
classPlayer{finalString name;// All final variables must be initialized, but 'name' isn't.
int xp;// Non-nullable instance field 'xp' must be initialized.Player(String name, int xp){// constructorthis.name = name;// 'name' can't be used as a setter because it's final.this.xp = xp;}}
// late 로 처리를 하면 위에서 발생한 에러가 해결된다.classPlayer{
late finalString name;
late int xp;// constructorPlayer(String name, int xp){this.name = name;this.xp = xp;}}// 훨씬 간결해진 표현classPlayer{// late 도 제거할 수 있다.finalString name;
int xp;// constructorPlayer(this.name,this.xp);// 중복선언된 타입을 제거할 수 있다.}
Named Constructor Parameters
classPlayer{finalString name;
int xp;String team;
int age;// The parameter {0} can't have a value of 'null' because of its type, but the implicit default value is 'null'.// Positional Constructor 에서 Named Constructor 로 변경하면 에러가 발생하게 되는데 required 키워드를 붙임으로써 해결Player({
required this.name,
required this.xp,
required this.team,
required this.age,});}voidmain(){// 파라미터 순서와 상관없이 값을 입력할 수 있음var player =Player(name:'moz', xp:12, team:'art', age:13);var player2 =Player(team:'tho', name:'ben', age:15, xp:200);}
Named Constructor
classPlayer{finalString name;
int xp, age;String team;Player({
required this.name,
required this.xp,
required this.team,
required this.age,});// team, xp 에 기본값을 설정한 Named Constructor with named parametersPlayer.createBluePlayer({required String name, required int age}):this.age = age,this.name = name,this.team ='blue',this.xp =0;// team, xp 에 기본값을 설정한 Named Constructor with positional parametersPlayer.createRedPlayer(String name, int age):this.age = age,this.name = name,this.team ='red',this.xp =0;}voidmain(){var player =Player.createBluePlayer(name:'moz', age:13);var player2 =Player.createRedPlayer('ben',15);}
Named Constructor 를 이용한 API 연동 예시
classPlayer{finalString name;
int xp;String team;Player.fromJson(Map<String,dynamic> playerJson): name = playerJson['name'],
xp = playerJson['xp'],
team = playerJson['team'];voidsayHello(){print("Hi my name is $name");}}voidmain(){var apiData =[{"name":"moz","xp":0,"team":"tiq"},{"name":"tho","xp":0,"team":"ven"},{"name":"shu","xp":0,"team":"ber"}];
apiData.forEach((playerJson){var player =Player.fromJson(playerJson);
player.sayHello();});}
Cascade Notation
classPlayer{String name;
int xp;String team;Player({
required this.name,
required this.xp,
required this.team,});voidsayHello(){print("Hi my name is $name");}}voidmain(){var moz =Player(name:'moz', xp:1000, team:'red');
moz.name ='tiq';
moz.xp =2000;
moz.team ='blue';
moz.sayHello();// Cascade NotationPlayer(name:'bee', xp:1000, team:'red')..name ='tho'..xp =3000..team ='green'..sayHello();}
Enums
// "red" 와 같이 문자열 형태로 쓰지 않아도 된다.enumTeam{ red, blue }enumXPLevel{beginner, medium, pro}classPlayer{String name;XPLevel xp;// enum XPLevel 타입으로 변경되었다.Team team;// enum Team 타입으로 변경되었다.Player({
required this.name,
required this.xp,
required this.team,});voidsayHello(){print("Hi my name is $name");}}voidmain(){var moz =Player(name:'moz', xp:XPLevel.beginner, team:Team.blue);
moz.sayHello();
moz
..name ='tho'..xp =XPLevel.pro
..team =Team.red
..sayHello();}
Abstract Classes
추상화 클래스로는 객체를 생성할 수 없다.
다른 클래스들이 직접 구현해야하는 메소드들을 모아놓은 일종의 청사진 (Blueprint)
특정 메소드를 구현하도록 강제한다.
abstractclassHuman{voidwalk();}classPlayerextendsHuman{// 구현하지 않으면 에러 발생voidwalk(){print('I am walking');}}
Inheritance
classHuman{finalString name;// Human(this.name); // positional parameterHuman({required this.name});// named parametersayHello(){print("Hi my name is $name");}}enumTeam{ red, blue }classPlayerextendsHuman{finalTeam team;/// 부모객체에서 필요한 값을 초기화// 부모객체의 생성자가 named parameter 인 경우Player({required this.team, required String name}):super(name: name);// 부모객체의 생성자가 positional parameter 인 경우// Player({required this.team, required String name}) : super(name);// super parameter 를 쓰는 것을 권장하고 있다.// Player({required this.team, required super.name});@overridevoidsayHello(){super.sayHello();print('and I play for ${team}');}}voidmain(){var player =Player(team:Team.red, name:'moz');
player.sayHello();}